2

I have been trying to study C about 3 days, and have a question about pointer/address

this is my code

#include <stdio.h>

#define EOL '\n'

int main()
{
    char one = 1;
    char two = 2;
    char three = 3;

    char* onePointer = NULL;
    char* twoPointer = NULL;
    char* threePointer = NULL;

    onePointer = &one;
    twoPointer = &two;
    threePointer = &three;

    printf("%cOne variables: %d%c", EOL, one, EOL);
    printf("Two variables: %d%c", two, EOL);
    printf("Three variables: %d%c", three, EOL);

    printf("%cOne adress: 0x%X%c", EOL, onePointer, EOL);
    printf("Two adress: 0x%X%c", twoPointer, EOL);
    printf("Three adress: 0x%X%c", threePointer, EOL);


    return 0;
}

when I try to compile it I get the following error "warning: format specifies type 'unsigned int' but the argument has type 'char *' [-Wformat]enter code here printf("%cOne adress: 0x%X%c", EOL, onePointer, EOL);" the same code does work on WINDOWS 7. (I have been watching youtube courses where the guy writes this code on win7)

If I changed %X to %p it compiles successfully "

One variables: 1
Two variables: 2
Three variables: 3

One adress: 0x0x7fff52381c0b
Two adress: 0x0x7fff52381c0a
Three adress: 0x0x7fff52381c09"

but if i run the output file again, every time i have different address it is ok?

One variables: 1
Two variables: 2
Three variables: 3

One adress: 0x0x7fff55b56c0b
Two adress: 0x0x7fff55b56c0a
Three adress: 0x0x7fff55b56c09

Oh, and one other question, I started my studies on WIN, then continued on mac os because looking in iOS dev side. So in windows I can create a function

char main()
void main(void)

whereas in Mac OS if recreate this example I get the error: " warning: return type of 'main' is not 'int' [-Wmain-return-type]" WHY?

Niall Cosgrove
  • 1,263
  • 1
  • 14
  • 24
kolqa_
  • 39
  • 3
  • 1
    You are **not** getting errors, you're getting warnings, which means your code still compiles. The fact that your windows compiler doesn't show them by default just makes it a bad compiler. – Siguza May 07 '16 at 17:26
  • He never said which compilers he was using, but Visual Studio on Windows prints out equivalent warnings for what he cited. – selbie May 07 '16 at 17:32
  • 2
    kolqa_, since you are new to C, here's a tip. Don't redefine `\n` as `EOL`. It only makes the code harder to read. `printf("\nOne variables: %d\n", one);` is a better written version of your first print statement. – selbie May 07 '16 at 17:34

2 Answers2

3

You should print pointers using %p not %X.

The fact that you see different addresses each time is normal. Read here: https://en.wikipedia.org/wiki/Address_space_layout_randomization

As for the signature of main(), it must return int. On all platforms. Other stuff is just some particular platform letting you do stuff you should not do.

John Zwinck
  • 207,363
  • 31
  • 261
  • 371
0

%X is a format specifier for hexadecimal integer. Depending on the platform architecture a hexadecimal integer may not be the same size as a pointer. Therefore use the pointer format specifier: %p so your code is more portable.

Search and read on printf format specifiers for more info.

It is overkill to provide a format specifier %c for line returns when those are constants, unlike variable values that change. Why make the code do extra work to write those in to the string when you can write \n in the format string directly? ( it would make sense if you thought you will later redefine EOL)

main() has a return type because it could be used to communicate the success or failure of your program to another invoking process, especially when your program is more of a command line tool rather than a gui driven one that visually communicates with the user. Think of how shell scripts invoke other tools and get back a result.

See also :

What should main() return in C and C++?

Pointers are addresses in memory. It should not be surprising that addresses differ between runs. In addition to the security reason cited, numerous factors influence what block of memory addresses are available to assign to variables in your method when it runs. Consider how memory available will change as you add more variables and methods to your program, not to mention how other processes on your machine are using memory at the moment. Think of it as getting a seat in a movie theater. What seat you can get depends on the current occupants in the theater.

Community
  • 1
  • 1