Pointer argument differences in C versus C++

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 703 Views - Last Post: 31 July 2013 - 06:17 PM Rate Topic: -----

#1 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Pointer argument differences in C versus C++

Posted 31 July 2013 - 12:43 PM

Why is this legal in C++, but not C?
#include <stdio.h>

char* pc;

void f(char* pc)
{
   //do nothing
}
int main()
{
    char* foo = NULL;
    f(&foo);
    return 0;
}



When I compile under c compiler it is fine. But with C++, it says, "cannot convert char** to char*"? No need to explain this, this part I understand. Why doesn't C restrict this behavior?

Note: When I compiled using cstdio versus stdio.h but sill using a C comiler via Codepad, I actually did receive the same error as I did compiling with C++. Can someone explain this?
Are there differences in headers.

This post has been edited by salazar: 31 July 2013 - 12:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pointer argument differences in C versus C++

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,109
  • Joined: 31-December 10

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 12:54 PM

The difference is in the compiler. Some compilers allow you set command-line arguments that indicate which language to use. Most compilers go by the file extension by default. In code you posted, you don't need to use the address-of (&) operator on a pointer to get to the address. If you just passed foo without any modifiers, it would be fine in both C/C++.
Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 12:56 PM

Because C++ is stronger typed than C.

A C++ compiler looks at the function call as you trying to pass an address to a pointer (which decays to a double pointer) to a function which only accepts a single pointer.

A C compiler will look at the function call and decays the address of the pointer to a single pointer.

The word pointer was thrown in a lot there so hopefully it's not too confusing :)/>

This post has been edited by jjl: 31 July 2013 - 12:59 PM

Was This Post Helpful? 1
  • +
  • -

#4 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 01:32 PM

View Postvividexstance, on 31 July 2013 - 12:54 PM, said:

The difference is in the compiler. Some compilers allow you set command-line arguments that indicate which language to use. Most compilers go by the file extension by default. In code you posted, you don't need to use the address-of (&) operator on a pointer to get to the address. If you just passed foo without any modifiers, it would be fine in both C/C++.


So when the compiler does not encounter the .h extension in cstdio it uses C++ instead of C?

View Postjjl, on 31 July 2013 - 12:56 PM, said:

Because C++ is stronger typed than C.

A C++ compiler looks at the function call as you trying to pass an address to a pointer (which decays to a double pointer) to a function which only accepts a single pointer.

A C compiler will look at the function call and decays the address of the pointer to a single pointer.

The word pointer was thrown in a lot there so hopefully it's not too confusing :)/>/>/>


So you are saying C automatically converts to a single pointer? Don't worry, I wasn't confused.

This post has been edited by salazar: 31 July 2013 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 01:42 PM

Quote

So when the compiler does not encounter the .h extension in cstdio it uses C++ instead of C?

No, it is usually the file extension of the file being compiled that makes a difference. You can usually force which compiler to use by telling your compiler to compile the file as either C or C++. How this is done varies with the compiler.

If you're compiling with the C compiler and you try to include a C++ standard header you will probably get quite a few different errors because C doesn't know how to handle C++ classes.

In either language the preprocessor looks for the exact file you provide. It doesn't really know anything about file extensions.


Jim

This post has been edited by jimblumberg: 31 July 2013 - 01:43 PM

Was This Post Helpful? 1
  • +
  • -

#6 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 02:23 PM

So how does that explain what happened when I changed the file from stdio.h to cstdio? I did not receive errors for stdio.h but got the same errors I would have if compiled using the C++ compiler, even though it was the C compiler.
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,109
  • Joined: 31-December 10

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 02:25 PM

Well cstdio defines everying inside the std namespace. What compiler are you using?
Was This Post Helpful? 1
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 02:35 PM

Well I suggest its your compiler

as these are the warnings I get with gcc MinGw x64 compiler.

Quote

C:\Workbench\errors\main.c||In function 'main':|
C:\Workbench\errors\main.c|12|warning: passing argument 1 of 'f' from incompatible pointer type [enabled by default]|
C:\Workbench\errors\main.c|5|note: expected 'char *' but argument is of type 'char **'|
||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|


I also got an error about the use of // as a remark instead of using the C /* ..*/ style remarks

which I removed.

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#9 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 03:13 PM

View Postvividexstance, on 31 July 2013 - 02:25 PM, said:

Well cstdio defines everying inside the std namespace. What compiler are you using?


I'm using codepad. On its site it list the compilers/interpreters used. C using gcc with
the following flags:
-O -fmessage-length=0 -fno-merge-constants -fstrict-aliasing -fstack-protector-all

C++ uses g++ with the following flags:
-O -std=c++98 -pedantic-errors -Wfatal-errors -Werror -Wall -Wextra -Wno-missing-field-initializers -Wwrite-strings -Wno-deprecated -Wno-unused -Wno-non-virtual-dtor -Wno-variadic-macros -fmessage-length=0 -ftemplate-depth-128 -fno-merge-constants -fno-nonansi-builtins -fno-gnu-keywords -fno-elide-constructors -fstrict-aliasing -fstack-protector-all -Winvalid-pch


And the only error i'm getting is the one abot char* cannot be converted to char** which is understandable.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 03:15 PM

Here are the compiler error messages I got when I compiled your code as either C or C++ with <stdio.h> and with <cstdio>

Quote

Compiling as C++ with <stdio.h>

main.cpp||In function ‘void f(char*)’:|
main.cpp|5|warning: no previous declaration for ‘void f(char*)’ [-Wmissing-declarations]|
main.cpp|5|warning: declaration of ‘pc’ shadows a global declaration [-Wshadow]|
main.cpp|3|warning: shadowed declaration is here [-Wshadow]|
main.cpp|5|warning: unused parameter ‘pc’ [-Wunused-parameter]|
main.cpp||In function ‘int main()’:|
main.cpp|12|error: cannot convert ‘char**’ to ‘char*’ for argument ‘1’ to ‘void f(char*)’|
||=== Build finished: 1 errors, 4 warnings (0 minutes, 0 seconds) ===|

Compiling as C with <stdio.h>

||warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C [enabled by default]|
main.cpp|5|warning: no previous declaration for ‘f’ [-Wmissing-declarations]|
main.cpp||In function ‘f’:|
main.cpp|5|warning: declaration of ‘pc’ shadows a global declaration [-Wshadow]|
main.cpp|3|warning: shadowed declaration is here [-Wshadow]|
main.cpp|7|error: C++ style comments are not allowed in ISO C90|
main.cpp|7|error: (this will be reported only once per input file)|
main.cpp|5|warning: unused parameter ‘pc’ [-Wunused-parameter]|
main.cpp||In function ‘main’:|
main.cpp|12|error: passing argument 1 of ‘f’ from incompatible pointer type|
main.cpp|5|note: expected ‘char *’ but argument is of type ‘char **’|
||=== Build finished: 3 errors, 5 warnings (0 minutes, 0 seconds) ===|

Compiling as C with <cstdio>

||warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C [enabled by default]|
main.cpp|1|fatal error: cstdio: No such file or directory|
||=== Build finished: 1 errors, 1 warnings (0 minutes, 0 seconds) ===|


Compiling as C++ with <cstdio>

main.cpp||In function ‘void f(char*)’:|
main.cpp|5|warning: no previous declaration for ‘void f(char*)’ [-Wmissing-declarations]|
main.cpp|5|warning: declaration of ‘pc’ shadows a global declaration [-Wshadow]|
main.cpp|3|warning: shadowed declaration is here [-Wshadow]|
main.cpp|5|warning: unused parameter ‘pc’ [-Wunused-parameter]|
main.cpp||In function ‘int main()’:|
main.cpp|12|error: cannot convert ‘char**’ to ‘char*’ for argument ‘1’ to ‘void f(char*)’|
||=== Build finished: 1 errors, 4 warnings (0 minutes, 0 seconds) ===|


Jim
Was This Post Helpful? 1
  • +
  • -

#11 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 03:23 PM

View Postsnoopy11, on 31 July 2013 - 02:35 PM, said:

Well I suggest its your compiler

as these are the warnings I get with gcc MinGw x64 compiler.

Quote

C:\Workbench\errors\main.c||In function 'main':|
C:\Workbench\errors\main.c|12|warning: passing argument 1 of 'f' from incompatible pointer type [enabled by default]|
C:\Workbench\errors\main.c|5|note: expected 'char *' but argument is of type 'char **'|
||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|


I also got an error about the use of // as a remark instead of using the C /* ..*/ style remarks

which I removed.

Regards

Snoopy.


How did you comiler? Was is using C configurations?
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 03:27 PM

Quote

C using gcc with
the following flags:

You really should have a few more flags. For example -Wall -Wextra -pedantic -pedantic-errors for C should be minimum.

I don't know if that compiler supports debugging but if it does then -g switch should also be added to generate debug symbols.


Jim

This post has been edited by jimblumberg: 31 July 2013 - 03:28 PM

Was This Post Helpful? 1
  • +
  • -

#13 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 03:41 PM

View Postsalazar, on 31 July 2013 - 10:23 PM, said:

View Postsnoopy11, on 31 July 2013 - 02:35 PM, said:

Well I suggest its your compiler

as these are the warnings I get with gcc MinGw x64 compiler.

Quote

C:\Workbench\errors\main.c||In function 'main':|
C:\Workbench\errors\main.c|12|warning: passing argument 1 of 'f' from incompatible pointer type [enabled by default]|
C:\Workbench\errors\main.c|5|note: expected 'char *' but argument is of type 'char **'|
||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|


I also got an error about the use of // as a remark instead of using the C /* ..*/ style remarks

which I removed.

Regards

Snoopy.


How did you comiler? Was is using C configurations?


Yes using C compiler as for using cstdio it didnt recognise the include as should your compiler.

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#14 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 03:43 PM

View Postjimblumberg, on 31 July 2013 - 03:15 PM, said:

Here are the compiler error messages I got when I compiled your code as either C or C++ with <stdio.h> and with <cstdio>

Okay, so not including irrelevant errors, C++ with the stdio.h (as well as the cstdio) compiles as expected.
The C with stdio compiles with errors, which I didn't expect. C with cstdio was not even able to find the file which makes sense. Maybe it has to do with compiler/settings. What compiler did you use?
Was This Post Helpful? 0
  • +
  • -

#15 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Pointer argument differences in C versus C++

Posted 31 July 2013 - 03:49 PM

View Postjimblumberg, on 31 July 2013 - 03:27 PM, said:

Quote

C using gcc with
the following flags:

You really should have a few more flags. For example -Wall -Wextra -pedantic -pedantic-errors for C should be minimum.

I don't know if that compiler supports debugging but if it does then -g switch should also be added to generate debug symbols.


Jim


Okay so it must be compiler dependent. Now, there is another question: is this acceptable behavior to allow the passing of an address to a function accepting a pointer?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2