pointer question.... pointer theory?

This is a discussion on pointer question.... pointer theory? within the C Programming forums, part of the General Programming Boards category; Ok, I am kind of confused here with pointers and arrays, I was wondering if someone could explain this to ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    pointer question.... pointer theory?

    Ok, I am kind of confused here with pointers and arrays, I was wondering if someone could explain this to me:

    char (*a)[5] - "a" is a pointer to an array of 5 characters

    char *a[5] - "a" is an array of pointers to characters

    actually, the 2nd one makes sense to me, because char* is the data type and we have created the a[5] array of this data type

    but the 1st line I do not understand.... what is the name of this array of 5 characters? Why does a point to it? what do the paranthese do?


    I appreciate any help with this, it's confusing to me. Thank you
    Last edited by panfilero; 11-11-2005 at 07:46 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    what is the name of this array of 5 characters?
    There is no name since the array doesn't exist. In this case 'a' is just a variable which can be assigned to the address of an array pointer. Proper usage of 'a might look like:

    Code:
    char (*a)[5];
    char b[5];
    a = &b;
    *a[0] = 'a';
    If you tried to assign an index value to the array 'a' points to without assigning it to an array first, you will get an error.

    The parenthesis are just used to distinguish the declaration from the second example you have listed there. It is similar as to when you declare a function pointer.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    so my pointer needs to be derefrenced twice? AS in this code:

    Code:
    #include <stdio.h>
    
    
    
    main()
    {
          char (*a)[5];
          char *h;     
          char abc[5] = "abcd";
          
          a = abc;
          
          
          printf("%c\n",a[0][2]);
          
           
          getchar();      
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    no, more like this:
    Code:
    int main(void)
    {
          char (*a)[5];
          char *h;     
          char abc[5] = "abcd";
          
          a = &abc;
          
          printf("%c\n",(*a)[2]);
          // or
          printf("%c\n",*((*a) + 2));
           
          return 0;    
    }

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I think that when you put
    Code:
    a = &abc;
    it should've been:
    Code:
    a = abc;
    since abc is an array with 5 elements and therefore abc is the pointer constant to the first element in the array.

    Right?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    no. When I put
    Code:
    a = &abc;
    I meant to put
    Code:
    a = &abc;
    'a' doesn't point to an array. It points to a pointer which points to an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 12:47 PM
  3. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. general pointer & malloc question
    By jstn in forum C Programming
    Replies: 2
    Last Post: 05-14-2002, 10:51 AM

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