(n00b) Pointer Confusion

This is a discussion on (n00b) Pointer Confusion within the C Programming forums, part of the General Programming Boards category; Hi, I am a relative newbie to C, although I am fairly experienced with Object Pascal. My question concerns pointers. ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    43

    (n00b) Pointer Confusion

    Hi, I am a relative newbie to C, although I am fairly experienced with Object Pascal. My question concerns pointers. First of all, I wrote the following test program:

    Code:
    #include <stdio.h>
    
    typedef char *CharPtr;
    
    CharPtr InitChar()
    {
    static char c;
    
    c = 'a';
    
    return &c;
    }
    
    int main ()
    {
      CharPtr c1;
    
      c1 = InitChar();
      printf("%c", *c1);
      
      getchar();
      return 0;
    }
    It compiled fine and the result "a" was printed on the screen. Now I wanted to modify my test program to use arrays instead of characters, so I wrote the following program:

    Code:
    #include <stdio.h>
    
    typedef int IntArrayTen[10+1];
    typedef IntArrayTen *ArrayPtr;
    
    ArrayPtr InitArray()
    {
    static IntArrayTen a;
    int i;
    
    a[0] = 0;
    
    for (i = 1; i <= 10; i++)
    {
    a[i] = i;
    }
    
    return &a;
    }
    
    int main ()
    {
      ArrayPtr aa;
    
      aa = InitArray();
      printf("%d", *(aa+4));
      
      getchar();
      return 0;
    }
    Now although I would expect this to print "4", instead it gave me some warnings and spat out a massive number. I found that I could solve the problem by changing
    Code:
    ArrayPtr aa
    to
    Code:
    ArrayPtr *aa
    However I do not understand why this works, because then aa is a pointer to a pointer to an array and so
    Code:
    *(aa+4)
    would be a pointer, which is not a valid parameter for the printf function.

    Hope you can help me here.

    Adam.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I'm not sure what the problem is with your program, but this works ok -- I removed the typedefs to make is clearer what InitArray function is returning.
    Code:
    #include <stdio.h>
    
    
    int* InitArray()
    {
    static int a[11];
    int i;
    
    a[0] = 0;
    
    for (i = 1; i <= 10; i++)
    {
    a[i] = i;
    }
    
    return a;
    }
    
    int main ()
    {
      int* aa;
    
      aa = InitArray();
      for(int i = 0; i <= 10; i++)
    	printf("%d ", aa[i]);
      
      getchar();
      return 0;
    }

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    look here
    Code:
    ArrayPtr InitArray()
    {
    static IntArrayTen a;
    int i;
    
    a[0] = 0;
    
    for (i = 1; i <= 10; i++)
    {
    a[i] = i;
    }
    
    return &a; // u are returning the address of the first array element which is wrong in your case. its just the 'a' itself instead of &a which gives u some other meaning
    }
    well let me explain. if u delcare an array for instance
    Code:
    int a[10];
    
    so when u say a[2]   it is some thing like *(a+2) where 2 is the offset.
    so when u add an offset to the base address the pointer will be moved in terms of bytes which actually depands on what datatype u have declared. say for instance 'int' 4 bytes will be incremented to the base address. i.e a+1 ( a + 4 bytes).

    hope this might help u

    ssharish2005

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    43
    Quote Originally Posted by ssharish2005
    look here
    Code:
    ArrayPtr InitArray()
    {
    static IntArrayTen a;
    int i;
    
    a[0] = 0;
    
    for (i = 1; i <= 10; i++)
    {
    a[i] = i;
    }
    
    return &a; // u are returning the address of the first array element which is wrong in your case. its just the 'a' itself instead of &a which gives u some other meaning
    }
    well let me explain. if u delcare an array for instance
    Code:
    int a[10];
    
    so when u say a[2]   it is some thing like *(a+2) where 2 is the offset.
    so when u add an offset to the base address the pointer will be moved in terms of bytes which actually depands on what datatype u have declared. say for instance 'int' 4 bytes will be incremented to the base address. i.e a+1 ( a + 4 bytes).

    hope this might help u

    ssharish2005
    So is there no essential difference between an array of integers, and a pointer to a particular integer, given that enough space has been allocated to store each element of the array? I discovered the problem; indeed, I had not realised that the type of a pointer to an array was in fact a pointer to the type the array is "of".

    I was going to ask about whether there is any way to get an array "back". So instead of having to write:

    Code:
    /*Assume int L, int a[L] and int* aa are defined*/
    
    int i;
    for (i = 1; i <= L; i++)
    {
    a[i] = aa[i];
    }
    after each function call, could we have some function or macro GetArrayBack and write:

    Code:
    a = GetArrayBack(aa, L);
    This gives some context to my question of the difference between an array of some type and a pointer to an element of the array.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    so if i understand your program correctly u dont need to return the array. b'cose arrays are sent to the function as call by refrence not call by value (which is diff). check out the program below

    Code:
    #include<stdio.h>
    #define MAX 10
    
    void init(int a[])
    {
         int i;
         
         for(i=0;i<MAX;i++)
         a[i]=i+1;
    }
    
    int main()
    {
        int a[MAX];
        int i;
        
        init(a);  // call by reference
        for(i=0;i<MAX;i++)
        printf("%d\t",a[i]);
        
        getchar();
        return 0;
    }
    /*myouput
    1       2       3       4       5       6       7       8       9       10
    */
    hope this might understand u better

    ssharish2005
    Last edited by ssharish2005; 10-17-2005 at 04:28 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kidburla
    So is there no essential difference between an array of integers, and a pointer to a particular integer, given that enough space has been allocated to store each element of the array? I discovered the problem; indeed, I had not realised that the type of a pointer to an array was in fact a pointer to the type the array is "of".

    I was going to ask about whether there is any way to get an array "back".

    ...

    This gives some context to my question of the difference between an array of some type and a pointer to an element of the array.
    What you need is a good FAQ.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Pointer confusion
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 12:44 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Pointer confusion...
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 10:18 PM

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