Quick Pointer Question

This is a discussion on Quick Pointer Question within the C Programming forums, part of the General Programming Boards category; I'm trying to solidify the concepts of various pointer types and I've run into a problem. I know that I ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    22

    Question Quick Pointer Question

    I'm trying to solidify the concepts of various pointer types and I've run into a problem.

    I know that I can initialize a pointer array (an array of pointers) with something like this:
    Code:
    char *dayArray[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" } ;
    But why is it that I cannot initialize a pointer to an array in the same format like this?
    Code:
    char (*dayArray)[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" } ;
    (instead of declaring the pointer and then assigning the address of a declared array separately)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    The first one is an array of pointers to char

    The second one is a pointer to an array of 7 chars.

    I wonder if this works...
    char (*dayArray)[7] = &"Monday";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    22
    Quote Originally Posted by Salem View Post
    The second one is a pointer to an array of 7 chars.

    I wonder if this works...
    char (*dayArray)[7] = &"Monday";
    That slipped by me. I initially was dealing with
    Code:
     int (*intArray)[4] = { 0, 1, 2, 3 } ;
    (which I still can't get to work) and decided to change the example to char at the last minute which changed things a bit.

    By the way your suggestion works exactly as you would think it would. Thanks.

    Quote Originally Posted by Elysia View Post
    String literals are unable to be modified because they point to the read-only string constant table. But, if you declare them const, you cannot change the pointer to another address.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    But, if you declare them const, you cannot change the pointer to another address.
    That is not true, unless you declare the pointer const. There is a difference between:
    Code:
    const char *p = "hello";       /* correct: non-const pointer to const */
    const char *const q = "world"; /* correct: const pointer to const */
    and:
    Code:
    char *p = "hello";       /* incorrect: non-const pointer to non-const */
    char *const q = "world"; /* incorrect: const pointer to non-const */
    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

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    22
    Thanks laserlight for the explanation. Your reasoning for using const for string constants was much better than the url wiki source given.

    So is it not possible to initialize a pointer to an int array on one line then (like the char pointers above)?
    This does not work:
    Code:
    int (*intArray)[4] = { 0, 1, 2, 3 } ;

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    You might be able to do something in C99
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    22
    Why does K&R decide to go into the fact that
    Code:
     int (*intArray)[10]
    is a pointer to an int array yet does not show you how this syntax would be used? I've tried declaring it the natural way I would with an int array which does not work. I've also tried defining a "int (*intArray)[10]" then declaring an int array and assigning the address to the pointer:
    Code:
    int main ( int argc, char *argv[] )
    {
    	int i ;
    	int myArray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
    	int (*pArray)[10] ;
    	pArray = myArray ;
    
    	while (*pArray) {
    		printf( "Values of pArray[] = %i\n", *pArray++ ) ;
    	}
    	
    }
    Maybe I'm doing something wrong in that regard? Maybe the only way is to actually define the pointer to the array: "int (*pArray)[10] ;" then allocate an int array of size 10 * int then assign it?

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by gwarf420 View Post
    Why does K&R decide to go into the fact that
    Code:
     int (*intArray)[10]
    is a pointer to an int array yet does not show you how this syntax would be used? I've tried declaring it the natural way I would with an int array which does not work. I've also tried defining a "int (*intArray)[10]" then declaring an int array and assigning the address to the pointer:
    Code:
    int main ( int argc, char *argv[] )
    {
    	int i ;
    	int myArray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
    	int (*pArray)[10] ;
    	pArray = myArray ;
    
    	while (*pArray) {
    		printf( "Values of pArray[] = %i\n", *pArray++ ) ;
    	}
    	
    }
    Maybe I'm doing something wrong in that regard? Maybe the only way is to actually define the pointer to the array: "int (*pArray)[10] ;" then allocate an int array of size 10 * int then assign it?
    That wouldnt print anything because the first value is 0...it wont pass the while() test.
    Also "int (*pArray)[10] ;" should be defined as "int *pArray;"

    EDIT: Hmm, wtf. I havnt compiled in C for a while (i use C++ compiler) but i cant do this???:

    Code:
    	for(size_t i = 0; i < sizeof(myArray) / sizeof(*myArray); ++i)
    	{
    		printf( "Values of pArray[] = %i\n", pArray[i]) ;
    	}
    It only compiles like this:

    Code:
    	size_t i;
    	for(i = 0; i < sizeof(myArray) / sizeof(*myArray); ++i)
    	{
    		printf( "Values of pArray[] = %i\n", pArray[i]) ;
    	}
    Last edited by 39ster; 06-01-2008 at 12:26 AM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
            int i;
            int myArray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
            int (*pArray)[10] ;
            pArray = &myArray;
    
            for (i=0; i<10; i++)
            {
                    printf( "Values of myArray[%i] = %i\n", i, (*pArray)[i] ) ;
            }
            return 0;
    }

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by gwarf420 View Post
    Code:
    int main ( int argc, char *argv[] )
    {
    	int i ;
    	int myArray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
    	int (*pArray)[10] ;
    	pArray = myArray ;
    
    	while (*pArray) {
    		printf( "Values of pArray[] = &#37;i\n", *pArray++ ) ;
    	}
    	
    }
    Maybe I'm doing something wrong in that regard? Maybe the only way is to actually define the pointer to the array: "int (*pArray)[10] ;" then allocate an int array of size 10 * int then assign it?
    Yeah, you're using pArray wrong. pArray++ will increment pArray by 40 bytes.

    Here's a better code example.
    Code:
    int main ( int argc, char *argv[] )
    {
    	int i ;
    	int myArray[2][3]= { {0, 1, 2}, {3, 4, 5} }; //I hope I didn't get 2 and 3 backwards
    	int (*pArray=&myArray[0])[3] ;
    
    	while(pArray<2) {
                    printf( "\nValue of myArray row : "
                    for( i=0;i<3;++i)
                           printf( "%i ", (*pArray)[i] ) ;
    	}	
    }
    Last edited by King Mir; 06-01-2008 at 01:24 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    22
    Quote Originally Posted by robwhit View Post
    Code:
            pArray = &myArray;
    Why do you assign the address of the array instead of just "myArray"? Isn't "myArray" the same as "&myArray[0]"?
    Quote Originally Posted by robwhit View Post
    Code:
            for (i=0; i<10; i++)
            {
                    printf( "Values of myArray[%i] = %i\n", i, (*pArray)[i] ) ;
            }
            return 0;
    }
    Apparently, you can only use pointer arithmetic if it is done normally: "int *pArray" instead of "int (*pArray)[10]"?

    BTW I'm sorry that this didn't turn out to be a "Quick Pointer Question" as I thought. But I do need to understand them fully.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by gwarf420 View Post
    Thanks laserlight for the explanation. Your reasoning for using const for string constants was much better than the url wiki source given.
    Unfortunately, it was your lack of understanding between pointer to const and const pointer to that caused some misunderstanding. Otherwise the article is fully understandable, I would say.

    But you must understand that
    int* pArray
    ...and...
    int (*pArray)[10]
    ...are not the same thing.

    The former is a pointer to an int and the latter is a pointer to an array of 10 ints.
    Also note that there is no bounds checking whatsoever, so even the former can be used as an array.
    The latter syntax may be necessary with two-dimensional strings or some other tricky stuff. Generally, it can be avoided.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Why do you assign the address of the array instead of just "myArray"? Isn't "myArray" the same as "&myArray[0]"?
    When converted to a pointer, myArray is indeed the same as &myArray[0], since an array is converted to a pointer to its first element. On the other hand, &myArray[0] is different from &myArray, since one is a pointer to the first element of myArray, while the other is a pointer to myArray itself. Although in value they may actually be the same, in terms of type they are different. Now, pArray is a pointer to an array of 10 ints, not a pointer to an int, so it is correct to assign &myArray to it.

    Apparently, you can only use pointer arithmetic if it is done normally: "int *pArray" instead of "int (*pArray)[10]"?
    No, pointer arithmetic makes sense when you have a pointer to some element in an array of elements (or to one past the end of an array). In this case, you have a pointer to a lone array of 10 ints, so pointer arithmetic does not make (much) sense.
    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

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    22
    Quote Originally Posted by Elysia View Post
    Unfortunately, it was your lack of understanding between pointer to const and const pointer to that caused some misunderstanding. Otherwise the article is fully understandable, I would say.
    Yes, but in explaining why char pointers should be declared const it should be natural to address the likely common question that "If I claim a char pointer as constant, it will not allow me to reassign the pointer?"

    I know, for me at least, it was not natural for me to think of a char pointer as two seperate types. (I viewed it as either a char pointer type or int pointer type or just an int. Not a pointer type pointing to an int or char type). A simple link in the article to that related concept would be nice. If it had not been for laserlight to take the extra step and explain that, I would have totally disregarded the advice because it would seem it would not allow me to reassign the char pointer (which would not be worth the hassle).

    But you must understand that
    int* pArray
    ...and...
    int (*pArray)[10]
    ...are not the same thing.
    Yes. The difference is explained in my OP. Just the syntactical use of such a construct was the question.

    Also note that there is no bounds checking whatsoever, so even the former can be used as an array.
    The latter syntax may be necessary with two-dimensional strings or some other tricky stuff. Generally, it can be avoided.
    Thanks for the info.

    Quote Originally Posted by laserlight View Post
    When converted to a pointer, myArray is indeed the same as &myArray[0], since an array is converted to a pointer to its first element. On the other hand, &myArray[0] is different from &myArray, since one is a pointer to the first element of myArray, while the other is a pointer to myArray itself. Although in value they may actually be the same, in terms of type they are different. Now, pArray is a pointer to an array of 10 ints, not a pointer to an int, so it is correct to assign &myArray to it.


    No, pointer arithmetic makes sense when you have a pointer to some element in an array of elements (or to one past the end of an array). In this case, you have a pointer to a lone array of 10 ints, so pointer arithmetic does not make (much) sense.
    So, a pointer to an array, is just that, a pointer to an array NOT a pointer to the first element of an array. Because of this, if I were to try and increment pArray later in the code, it would be moving the pointer the full length of the array instead of each element of the array.

    As I understand it, the only real reason to use this construct is using multidimensional arrays and allow to increment by a group of elements at one time rather than each element individually?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  2. Quick Pointer question
    By Sentral in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2006, 08:36 PM
  3. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:36 AM
  4. Declaring a Pointer (quick question)
    By viciousv322 in forum C Programming
    Replies: 4
    Last Post: 12-16-2005, 11:27 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

Tags for this Thread


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