tags:

views:

153

answers:

4

I am learning C programming and I have a simple question about pointers...

I used the following code to play around with pointers:

#include <stdio.h>

int main (int argc, const char * argv[]) {

int * c;

printf("%x\n",c);
return 0;
}

When I print the value of C, I get back a 0. However, when I print &c (i.e. printf("&x\n",&c) I get an address in memory...

Shouldn't I be getting an address in memory when printing the pointer (i.e. printf("%x\n",c)?

--- EDIT ---

#include <stdio.h>
#include <stdlib.h>

int main (int argc, const char * argv[]) {

    char * a = malloc(11);
    printf("String please\n");
    scanf("%s",a);
    printf("%s",a);

}

The question is, why does printf("%s",a) returns the string instead of the address that is stored in a?

Shouldn't I use *a to follow the pointer and then print the string?

+9  A: 

your current program is not correct. You define variable and do not set value before first use. the initial value is not guranteed for c, but you are lucky and it is equal to 0. It means that c points to nowhere. when you print &c you print address of variable c itself. So actually both versions print address.

Andrey
alexBrand
@alexBrand yes, every variable has an address, even if it is pointer. you can define pointer to pointer to int: `int **pc = `
Andrey
thanks a lot. So basically the pointer is initialized but its not pointing anywhere... Is this because of luck or it is automatically initialized to NULL?
alexBrand
@alexBrand it is not initialized, and it must be intialized by you before first use. no automatic initialization.
Andrey
got into some trouble again... if you wouldn't mind check the new code I added. Thanks
alexBrand
@alexBrand it works that way because %s treats parameter as pointer to string and print string. if you use %x you will see the address
Andrey
A: 

Having int* c; If you print value of c, you get back a value that should be interpreted as a memory address of an integer value. In you example it might be 0 or something completely different as you are not initializing c.

If you print &c you get memory address of the pointer c (stored in stack).

Jari
A: 
#include <stdio.h>

int main (int argc, const char * argv[]) {

int * c;
int a=10;
c = &a;

printf("%x\n",c);
return 0;
}

This may clarify what happens when you make the int pointer point to something in memory.

Praveen S
+1  A: 

printf is actually quite a complex function and can be made to do all sorts of tricks by giving it the right format specifier.

In your string example:

printf("%s", a)

the "%s" tells the printf function that the following variable should be treated as a string. In C, a string is a pointer to one or more char, terminated by a char containing 0. This is a pretty common request, which is why printf supports a format specifier "%s" that triggers this relatively complex behavior. If you want to print the address contained in the string pointer you have to use the format you found earlier:

printf("%x\n",a);

which tells printf to treat the contents of a as an unsigned integer, and print it in base 16.

*a would just be the value pointed to by a, which is just a single character.

You could print the single character with

printf("%c", *a);
Charles E. Grant
thanks a lot. So basically printf figures out what you are trying to print, and will then decide if it should follow the pointer or not?
alexBrand
That's correct.
Charles E. Grant
I'd add that C assumes that you known what you're doing, and won't try to stop you from shooting yourself in the foot. For example:int i = 10;printf("%s", i);That is, "declare an integer variable i, set it to 10, then print i treating as if it were a string"This is valid C syntax and will compile without an error (though almost all modern compilers will issue a warning). When you try to run the program you'll get a segmentation fault, because 10 won't be a valid value for a memory address.
Charles E. Grant