pointers and arrays

This is a discussion on pointers and arrays within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to understand the following code. Code: typedef struct x { int a; char c; fload f; }stx; ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    1

    pointers and arrays

    Hi,
    I need to understand the following code.

    Code:
    typedef struct x
    {
      int a;
      char c;
      fload f;
    }stx;
    
    struct upper
    {
      int num;
      stx *pstx;
    }
    
    now,here *pstx is a pointer to a structure stx.
    1.if we write stx *pstx[10];here pstx is an array of pointers to the structure stx.
       to allocate the memory pstx[0] = new stx;
    
    2.if pstx = new stx[10].
    then
    Coulde somebody tell me the difference between 1 and 2.

    Best Regards
    Murali026

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    1.if we write stx *pstx[10];here pstx is an array of pointers to the structure stx.
    to allocate the memory pstx[0] = new stx;
    True, but this will only allocate memory dynamically to the first element in the array.

    2.if pstx = new stx[10].
    Code:
    stx *pstx = new stx[10];
    will allocate memory dynamically for an array of 10 structs and return that memory to the pointer pstx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 02:41 AM

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