1,105,281 Community Members

Pointer Arithmetic Problem

Member Avatar
Graphix
Posting Pro in Training
410 posts since Aug 2009
Reputation Points: 68 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
}
Member Avatar
Graphix
Posting Pro in Training
410 posts since Aug 2009
Reputation Points: 68 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

Sure:

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

~G

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
1
 

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

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Unknown (without asterisk): 4210883
Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Graphix
Posting Pro in Training
410 posts since Aug 2009
Reputation Points: 68 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the quick replies!

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

Question Answered as of 2 Years Ago by gerard4143
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: