Pointers

This is a discussion on Pointers within the C Programming forums, part of the General Programming Boards category; Does char *fld[NUMFLD] mean I have a pointer to a array of strings? So NUMFLD will contain strings located at ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Pointers

    Does char *fld[NUMFLD] mean I have a pointer to a array of strings? So NUMFLD will contain strings located at NUMFLD 0,1,2 and so on... I then reference them with my pointer like *fld[1], fld[2]... this has got me all confused after I thought I was making progress. Can someone tell me what this means and give me a example in how it works. Thanks.

    -Carl

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by cjohnman View Post
    Does char *fld[NUMFLD] mean I have a pointer to a array of strings?
    No -- there's really no such thing as a "pointer to an array." The declaration means an array of pointers (to strings, presumably). fld[0] points to the first string, fld[1] to the second, etc.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Quote Originally Posted by brewbuck View Post
    No -- there's really no such thing as a "pointer to an array."
    There is such a thing, but it's rarely used, in my experience. C sneaks them in behind your back when you're passing multidimensional arrays to functions:
    Code:
    void f(int x[][5]);
    Here x is actually a pointer to an array of 5 int. This could also be written:
    Code:
    void f(int (*x)[5]);
    You can, of course, also explicitly use pointers to arrays in your code, though I don't ever recall having to do this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int a[1] = { 5 };
      int (*p)[1] = &a;
    
      printf("%d\n", (*p)[0]);
    
      return 0;
    }
    Not especially useful, but there you are. If you mentioned that there's really no such thing as pointers to arrays in order to not confuse those new to C, sorry for blowing the operation.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    No, there's no such thing as a pointer to an array. Even your example is a plain pointer. You're just using a different syntax to tell the compiler the dimensions of the array.
    You can see this for more info:
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    Last edited by Elysia; 04-10-2008 at 11:52 PM.
    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.

  5. #5
    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.*

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Elysia View Post
    No, there's no such thing as a pointer to an array.
    Or, of course, there's this:
    Quote Originally Posted by ISO C, 6.5.2.1
    Successive subscript operators designate an element of a multidimensional array object.
    If E is an n-dimensional array (n 2) with dimensions i j . . . k, then E (used as
    other than an lvalue) is converted to a pointer to an (n - 1)-dimensional array with
    dimensions j . . . k. If the unary * operator is applied to this pointer explicitly, or
    implicitly as a result of subscripting, the result is the pointed-to (n - 1)-dimensional array,
    which itself is converted into a pointer if used as other than an lvalue.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Of course, I didn't think of allocating an array on the heap which would technically make it a pointer to an array.
    Well, whatever goes. I don't know if I want to call cas's example as a pointer to an array because that's technically wrong. It's defined as a pointer to an array, yet it's a multi-dimensional array - types do not agree.
    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.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Elysia View Post
    Of course, I didn't think of allocating an array on the heap which would technically make it a pointer to an array.
    Well, whatever goes. I don't know if I want to call cas's example as a pointer to an array because that's technically wrong. It's defined as a pointer to an array, yet it's a multi-dimensional array - types do not agree.
    What are you talking about?
    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.*

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Code:
    void f(int (*x)[5]);
    Looks to my like a pointer a array of 1 dimension with 5 elements (though it may not be, deceiving syntax).
    And in the end, it's just a simple pointer that's passed to the function because the address of the start of the array is passed, so technically it's just a pointer.
    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.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Type is important for things such as ++x.
    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.*

  11. #11
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    If you read the C standard, it's quite clear that pointers to arrays exist, and that what I posted is an example of one. However, you must first acknowledge that pointers and arrays are separate things, I suppose, and if you do not do that, it would be hard to convince you that a pointer to an array can exist.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    I see things from a lower perspective. An array is just a block of memory. And to address that block of memory, a pointer is required. Although if it's placed on the stack, then the compiler just needs to move memory from a stack pointer offset, wheras if the array is passed to another function, the address to the start of the array is passed, thus effectively making it a pointer that must be dereferenced first.
    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.

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. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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