We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,263 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Pointer Arithmetic Problem

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
2
Contributors
6
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
Graphix
Posting Pro in Training
434 posts since Aug 2009
Reputation Points: 82
Solved Threads: 75
Skill Endorsements: 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;
}
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0

Sure:

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

~G

Graphix
Posting Pro in Training
434 posts since Aug 2009
Reputation Points: 82
Solved Threads: 75
Skill Endorsements: 0

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

gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0

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

Unknown (without asterisk): 4210883
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 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.

gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0

Thanks for the quick replies!

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

Graphix
Posting Pro in Training
434 posts since Aug 2009
Reputation Points: 82
Solved Threads: 75
Skill Endorsements: 0
Question Answered as of 1 Year Ago by gerard4143

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0758 seconds using 2.7MB