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

Recommended Answers

All 6 Replies

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;
}

Sure:

size->4
size->4
size->0022FF4C
size->2293580

~G

Sure:

size->4
size->4
size->0022FF4C
size->2293580

~G

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".

commented: Thanks for the help +6

I would say that this is the value of the address held by p3

Unknown (without asterisk): 4210883

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.