Start New Discussion within our Software Development Community


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 :)


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

Could you post the results of running this code.

#include <stdio.h>

int main()
  int x;
  fprintf(stdout, "size->%lu\n", sizeof(void*));
  fprintf(stdout, "size->%lu\n", sizeof(int));
  fprintf(stdout, "size->%p\n", (void*)&x);
  fprintf(stdout, "size->%d\n", (int)&x);

  return 0;




I just wanted to make sure you weren't truncating anything..

Now lets look at

int * p3 = "47352";//assign to pointer p3, the pointer that points to "47352"

Unknown (with asterisk): 892548916

*p3 produces 892548916

now 892548916 has a hex value of 0x35333734 which has ascii values

0x35 = '5'
0x33 = '3'
0x37 = '7'
0x34 = '4'

notice that p3 points to "47352" which is an int pointer and int are four bytes on your system so your getting the first four characters of the string "47352".

Thanks for the help

And the last one

char * p2 = "Some string";
printf("Unknown (with asterisk): %d\n", *p2);

Unknown (with asterisk): 83

dereferncing a character pointer returns the character pointed to which is 'S' and the numeric(ascii) value of 'S' is 83.

Thanks for the quick replies!

I now understand the values of pointers a bit more :)

This question has already been answered. Start a new discussion instead.