Hi,
I've been trying to learn what pointers are and how they function, and I now finally understand most of it. However, I don't understand the assignment of strings to a char *, and what the values they have mean. I wrote a small program to learn:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a = 60;
int * p;
p = &a;
printf("Value (with asterisk): %d\n", *p);
printf("Location (without asterisk): %d\n\n", (int) p);
char a1 = 'c';
char * p1 = &a1;
printf("Value (with asterisk): %c\n", *p1);
printf("Location (without asterisk): %d\n\n", (int) p1);
char * p2 = "Some string";
printf("Unknown (with asterisk): %d\n", *p2);
printf("Location (with ampersand): %d\n", (int) &p2);
printf("Value (without asterisk): %s\n\n", p2);
int * p3 = "47352";
printf("Unknown (with asterisk): %d\n", *p3);
printf("Location (with ampersand): %d\n", (int) &p3);
printf("Unknown (without asterisk): %d\n", (int) p3);
return 0;
}
I've marked the values I don't understand as 'Unknown'. It seems as if a char * that has a string assigned to it, is sort of the same as a regular type (like int), because it it's value is p2 and it's location &p2. But I don't understand how. I also tryed the same trick with a int *, but the results are not the same (the 'without asterisk' is not the correct value of 47352)
I've googled many tutorials, but none can explain how this really works! If someone could send me a tutorial that does, it would really help me out :)
~G
PS: The output on my computer:
Value (with asterisk): 60
Location (without asterisk): 2293572
Value (with asterisk): c
Location (without asterisk): 2293571
Unknown (with asterisk): 83
Location (with ampersand): 2293564
Value (without asterisk): Some string
Unknown (with asterisk): 892548916
Location (with ampersand): 2293560
Unknown (without asterisk): 4210883