Thread: Pointers

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Pointers

    Hi,

    I am studying C (and pointers) now, I found a nice tutorial site (with excercises) here. I am now trying to solve the excercise under "Pointers" chapter:

    Write a C program to read through an array of any type using pointers.
    Right now, I have this:

    Code:
    #include <stdio.h>
    
    int main(int argc, char**argv)
    {
    	int arr1[10] = {1,2,3,4,5,6,7,8,9,10};
    	char s[10] = {'a', 'b', 'c', 'd', 'e', 'd', 'c', 'b', 'a', 'z'};
    	char *p = s;
     	while (*p != '\0')
    	{
    		printf("&#37;c", s[p-s]);
    		p++;
    	}
    	
    	return 0;
    }
    
    // this outputs: abcdedcbaz
    It reads through a CHAR array (and displays the values), but how can I read through ANY array using 1 (the same) pointer? Or is that impossible? (I don't understand C and pointers very well yet)

    Edit: Is a void* pointer the solution?
    Last edited by Ideswa; 05-01-2007 at 02:07 PM.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    I'd consider using a different loop method. First off, there's no guarantee that the 11th character of the s array will be a null terminator, since you only allocated 10 characters, so it's undefined after that.
    Since you know that there are 10 characters in the array, I'd encourage you to use a for loop:
    Code:
    for (i = 0; i < 10; ++i)
    {
    }
    as for what to print out, just print out whatever p points at:
    Code:
    printf("&#37;c", p[ i ]);
    and then that would work for a pointer to arr1 as well, provided you change the %c in the printf to the escape for an integer.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    #include <stdio.h>
    
    int main(int argc, char**argv)
    {
    	int arr1[10] = {1,2,3,4,5,6,7,8,9,10};
    	char s[10] = {'a', 'b', 'c', 'd', 'e', 'd', 'c', 'b', 'a', 'z'};
    	int i;
    	char *p = arr1;
     	for(i = 0; i < 10; i++)
    	{
    		printf("%d", p[i]);
    	}
    	
    	return 0;
    }
    this outputs: 1000200030

    ?? :P

    By the way: Do you think it's useful to learn C, (I can code C++ pretty good right now, also OOP and stuff).
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    That's because you're pointing a char pointer to an int. The data at the address of the int will be interpreted as a char. You will either have to change the pointer type or type cast the pointer before derefering it.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
     	for(i = 0; i < 10; i++)
    	{
    		printf("&#37;d\n", p[i * sizeof(int)]);
    	}
    the size of each element in a char array is 1 byte. however the size of each element in an int array is different, usually 4 (use 'sizeof(int)' to be safe).
    when your looping through the array your basically taking the address of the first item, and adding 1 to it. thats fine for a char array. but when you point your char array to an int array, you have to increment each time by 'i * sizeof(int)'.

    hope it helps.

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    That would only "grab" the first byte of the integer, not all (usually 4) as necessary.
    It would have to be done as
    Code:
    *(int*) (p + i*sizeof(int))
    or
    Code:
    (int*) p[i]

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    good point, thanks.
    however:
    Code:
    printf("&#37;d\n",(int*) p[i]);
    doesnt work.
    edit: the other one does though, which i should have added in my first code.
    Last edited by nadroj; 05-01-2007 at 02:54 PM.

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I thought type-casting and the [] operator had the same priority and evaluated from the left.
    EDIT: I think I mixed it up with the grouping operator, (). type-casting is below [].
    Code:
    ((int*) p)[i]
    Should work though.
    Last edited by OnionKnight; 05-01-2007 at 02:59 PM.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok thanks, interesting.

  10. #10
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Thanks for the replies! Learned a lot again. C is more complicated than C++, I think!
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM