Thread: pointers and arrays

  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, 01:41 AM