Array of pointers

This is a discussion on Array of pointers within the C Programming forums, part of the General Programming Boards category; I cannot get it work. The function returnPointer() returns pointer for every string I've inserted until it returns null value. ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    19

    Array of pointers

    I cannot get it work.
    The function returnPointer() returns pointer for every string I've inserted until it returns null value. I want to store all of these returned pointers in a dynamic array named arrayOfPointers in the way that I can print out all these stored strings later. The main function doesn't work correctly. It just crashes (altough it seems to store some pointers in arrayOfPointers). I think that there is something wrong with variable q after passing through the while loop in the main function for the first time.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    char * returnPointer();
    
    int main ( void )
    {
      char *q = NULL;
      char **arrayOfPointers = NULL;
      int size2 = 0;
      int capacity2 = 0;
      do 
      {
        if ( size2 == capacity2 ) 
        {
          capacity2 += 1;
          arrayOfPointers = realloc ( q, capacity2 + 1 );
        }
        q = returnPointer();
        arrayOfPointers[size2++] = q;
        //printf("%d\n",arrayOfPointers[size2]);
      } while (q != NULL);
      free(arrayOfPointers);
      return 0;
    }
    
    char * returnPointer()
    {
      char *s = NULL;
      int size = 0;
      int capacity = 0;
      int ch;
      printf("Insert something\n");
      while ( ( ch = getchar() ) != '\n' && ch != EOF ) 
      {
        if ( size == capacity ) 
        {
          char *save;
          capacity += 5;
          save = realloc ( s, capacity + 1 );
          if ( save == NULL )
            break;
          s = save;
        }
        s[size++] = ch;
      }
    
      if ( size > 0 ) 
      {
        s[size] = '\0';
      }
      return s;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > arrayOfPointers = realloc ( q, capacity2 + 1 );
    1. Follow the same idea in the called function.
    You want arrayOfPointers = realloc ( arrayOfPointers, capacity2 + 1 ); with the temp variable.

    2. You need to multiply the amount by the size.
    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
    Registered User
    Join Date
    Nov 2008
    Posts
    19
    Quote Originally Posted by Salem View Post
    > arrayOfPointers = realloc ( q, capacity2 + 1 );
    1. Follow the same idea in the called function.
    You want arrayOfPointers = realloc ( arrayOfPointers, capacity2 + 1 ); with the temp variable.

    2. You need to multiply the amount by the size.
    1. Do you mean variable 'save' by variable 'temp' as an example from called function?
    2. I don't understand what you mean by that sentence. I cannot distinguish amount and size at the moment.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    2) Malloc works on a byte-basis, ie raw data. Is does not know of types.
    Since an int is typically 4 bytes, then logically, you would need 40 bytes to store 10 integer, no?
    10 is the amount, 4 is the size.
    So multiply the amount by the size and you get enough storage.
    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
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    The general form for all the memory allocation calls is
    p = malloc ( howMany * sizeof(eachElement) );

    Which can be conveniently written as
    p = malloc ( howMany * sizeof(*p) );

    realloc works in exactly the same way.

    calloc is a bit easier, as it expects two parameters, so
    p = calloc ( howMany, sizeof(*p) );


    > 1. Do you mean variable 'save' by variable 'temp' as an example from called function?
    I mean the way you used 'save' to protect yourself against a memory leak should realloc return NULL.
    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.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    19
    Ok, it seems to work now. Just want to know why there is no need to multiply amount by size in returnPointer() function as I did in the main function?
    Code:
    int main ( void )
    {
      char *q = NULL;
      char **arrayOfPointers = NULL;
      int size2 = 0;
      int capacity2 = 0;
      int i=0;
      do 
      {
        if ( size2 == capacity2 ) 
        {
          char **save2;
          capacity2 += 1;
          save2 = realloc (arrayOfPointers, (capacity2 + 1)*sizeof(save2) );
          if ( save2 == NULL )
            break;
          arrayOfPointers = save2;
        }
        q = returnPointer();
        arrayOfPointers[size2++] = q;
        printf("%s\n",*(arrayOfPointers+i));
        i++;
      } while (q != NULL);
      free(arrayOfPointers);
      return 0;
    }

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Well, the size of a char is always 1 and something multiplied with 1 is always something (same answer).
    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
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > save2 = realloc (arrayOfPointers, (capacity2 + 1)*sizeof(save2) );
    No.
    save2 = realloc (arrayOfPointers, (capacity2 + 1)*sizeof(*arrayOfPointers) );


    > free(arrayOfPointers);
    You also need to free each arrayOfPointers[i] before you free the overall pointer.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 05:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 06:13 AM

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