Like Tree2Likes
  • 1 Post By tabstop
  • 1 Post By laserlight

Pointing to the next character in an array?

This is a discussion on Pointing to the next character in an array? within the C Programming forums, part of the General Programming Boards category; Example: Code: int myArray[] = {5, 6, 7, 8}; int *myPointer = &myArray[0] Why is the following true? Code: *(myPointer+1) ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    16

    Pointing to the next character in an array?

    Example:
    Code:
    int myArray[] = {5, 6, 7, 8};
    int *myPointer = &myArray[0]
    Why is the following true?
    Code:
    *(myPointer+1) == 6
    I get the concept, but shouldn't this only hold true if the variable is a one-byte variable? Using my logic, I would use this instead:
    Code:
    *(myPointer+sizeof(int)) == 6
    Is it just that C compilers automatically take the size of the variable being pointed to into account?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Print the address of the variable in question, and then print the address of the variable + 1, and you will have your answer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    16
    Quote Originally Posted by quzah View Post
    Print the address of the variable in question, and then print the address of the variable + 1, and you will have your answer.


    Quzah.
    What's the best way to do this? Pretty much any way I try it, I get what I expected -- the compiler automatically takes into account the size of the variable. However, now I'm curious. Here's my code:
    Code:
    #include <stdio.h>
    int main ()
    {
    	int myArray[] = {5, 6, 7, 8};
    	int * myPointer = &myArray[0];
    	printf("myPointer:\t0x%08lX\n", (unsigned long)myPointer);
    	printf("myPointer+1:\t0x%08lX\n", (unsigned long)(myPointer+1));
    	return 0;
    }
    If I remember correctly, the size of a pointer, if you will, is dependent on the architecture? A 32-bit architecture has a pointer (i.e. address) size of 4 bytes, while a 64-bit architecture will have a pointer size of 8 bytes?

    In other words, the maximum possible address location on a 32-bit architecture would be 0xFF FF FF FF, and on a 64-bit architecture it would be 0xFF FF FF FF FF FF FF FF.

    Is that correct?

    Then, further, is it safe to always use unsigned long for the size of a pointer? In other words, is the size of the unsigned long variable type interpreted differently on different machines with different architectures? Is this size determined at compilation or at runtime?

    Thank you in advance.

    Edit: I just discovered %p as a printf() specifier, so I'll look into that. But my previous curiosity still holds! Any advice on either part is appreciated.
    Last edited by steez; 06-01-2011 at 08:03 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The whole thing also depends on your libraries and compiler. For instance, using MinGW on my 64-bit Windows 7 machine still gives sizeof(long)==4 and sizeof(long*)==4.
    caroundw5h likes this.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use %p and type cast to (void *) to print addresses. The address of the next member in an array of type will be current address + sizeof( type ).


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    16
    Edit: nevermind. I couldn't get the %x version to work the same as the %p version, but now I have:

    Code:
    #include <stdio.h>
    int main ()
    {
    	int myArray[] = {5, 6, 7, 8};
    	int * myPointer = &myArray[0];
    
    	printf("myPointer:\t0x%08lx\n", (unsigned long)myPointer);
    	printf("myPointer:\t%p\n", (void*)&myArray[0]);
    	return 0;
    }
    Last edited by steez; 06-01-2011 at 08:26 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    Notice the difference between printing the value of a pointer (in this case converted to unsigned long) and printing the value of the address of a pointer. It is similiar to the difference between printing the value of an int variable and printing the address of the int variable.
    quzah likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    16
    Yes, I made a mistake, I thought the %p took care of that for me (I thought I was supposed to pass the pointer's address to it for some reason, which makes no sense in retrospect).
    I edited my above post, thanks.

    Edit: any recommendations or advice?
    Code:
    #include <stdio.h>
    int main ()
    {
    	int myArray[] = {5, 6, 7, 8};
    	int * myPointer = &myArray[0];
    
    	printf("Print address of beginning of array of 4-byte integers:\n");
    
    	// print address of myArray[0]
    	printf("&myArray[0]:\t0x%08lx\n", (unsigned long)myPointer);
    	printf("&myArray[0]:\t%p\n", (void*)&myArray[0]);
    	printf("&myArray[0]:\t%p\n", (void*)myPointer);
    
    	// print address of next element of the array
    	printf("&myArray[1]:\t0x%08lx\n", (unsigned long)(myPointer+1));
    	printf("&myArray[1]:\t%p\n", ((void*)&myArray[0])+sizeof(int));
    	printf("&myArray[1]:\t%p\n", ((void*)myPointer)+sizeof(int));
    	return 0;
    }
    Last edited by steez; 06-01-2011 at 08:33 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    The whole thing also depends on your libraries and compiler. For instance, using MinGW on my 64-bit Windows 7 machine still gives sizeof(long)==4 and sizeof(long*)==4.
    If you compile for an x64 target sizeof(long*) should return 8... x64 pointers are 64bits (or should be).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array and pointing randomly
    By gloworm in forum C Programming
    Replies: 4
    Last Post: 04-12-2010, 12:03 PM
  2. pointing to an array
    By gloworm in forum C Programming
    Replies: 4
    Last Post: 04-11-2010, 11:46 AM
  3. Replies: 6
    Last Post: 11-03-2009, 12:23 PM
  4. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  5. array of pointers each pointing to an array
    By muttski in forum C Programming
    Replies: 6
    Last Post: 05-01-2002, 02:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21