Thread: Arrays of pointers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    Arrays of pointers

    Hi,

    I'm the middle of writing a program which will store a list of names in dynamic memory. How can i let the user define the number of names in the list ie size of the array, without using type constant? or is that even possible?

    Regards
    Ryan

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Totally possible... Linked lists were made for just this purpose... an array without end.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    How do i create an array with no end? ive tried leaving the [] empty but i get an error

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by CommonTater
    Totally possible... Linked lists were made for just this purpose... an array without end.
    I think I'm going to have to disagree with that. If there was no way of telling at the time of allocation how many names there would be then a linked list might be a good solution. But if you know up front how many names there will be I would just do a simple malloc():

    Code:
    int main(void)
    {
      int num_names = 7; // You'd get this number from the user in reality
      char **array = malloc(sizeof(*array) * num_names);
      if(!array)
      {
        // Memory allocation failed. Due something ingenious.
      }
    
      array[0] = "Steve";
      array[1] = "Beth";
    
      // Free the memory when you're done with it!
      free(array);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Look up malloc(). If it's a char array maybe you'd want a 2-dimensional array with its width being the longest name + 1 for null terminator. Or if you want to have an array of char pointers then that's just a single dimension array.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    yea, I used calloc() in my last program, which was to store ints. But it's proving difficult to use the same methods but only to store names instead :S

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So why not show us what you've tried?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itsme86 View Post
    I think I'm going to have to disagree with that. If there was no way of telling at the time of allocation how many names there would be then a linked list might be a good solution. But if you know up front how many names there will be I would just do a simple malloc():
    So would I... but when it's a total unknown, as I understood this to be, the joy of linked lists is that you can just keep creating new structs and adding to them.

    If the operator (correctly) reports the number of names... an array is the way to go.
    If the length of the names is controlled... a 2D array of strings is the way to go.
    But if you don't know these things... it's linked list time.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    no problemo, this is what i have so far but the values returned equals null, suspecting the memory is not allocated to begin with. i know its no where near a completed program for what i want it to do, but some guidance of what to add would be more than appreciated.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    	// Declare variables
    	int size; // Will later be assigned to a user defined value to store the size of the array.
    	char *pNumber;// The pointer
    
    	// Asks user how many names they would like to enter
    	printf("How many names would you like to store? ");
    	scanf_s("%i", &size);
    
    	// pointer assigned array, and memory is alocated to store elememts on the array
    	pNumber = (char *) calloc(size, sizeof(int));
    
    	// Error displayed if user inputs a number of elements which cannot be catered for by memory
    	if(pNumber == NULL)
    	{
    	printf("Not enough memory!!\n");
    	}
    
    	// A loop to store the elements, as the user inputs them, into the array.
    	for(int i = 0; i < size; i++)
    	{
    		printf("Enter an name to be stored: ");
    		scanf_s("%s", (pNumber+i));
    		printf("\n");
    	}
    	// A loop to re display the elements of the array in a reverse order.
    	for(int i = size -1; i >= 0; i--)
    	{
    	printf("%s \n\n", *(pNumber+i));
    	}
    
    	//Releases the memory
    	free(pNumber);
    }

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RyanLeonard View Post
    How do i create an array with no end? ive tried leaving the [] empty but i get an error
    Well it's not actually an array at all... The premise is that you define a struct to hold your data. In the struct is a pointer to the next struct... So you only have to keep a head pointer. It's not entirely simple but it's very powerful...

    1) Malloc space for the first struct as your head pointer... fill in the data.
    2) Malloc space for the next struct as the pointer in the first struct... fill in the data.
    3) Repeat step 2 until all data is listed.
    ...
    4) When done start at the last struct and free your way down the chain.


    Cprogramming.com Tutorial: Linked Lists

    It might not be the answer here but it's a very interesting construct.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    My next practice program is using structs, although in this one i have to use arrays of pointers. thanks for the suggest though CommonTater

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RyanLeonard View Post
    My next practice program is using structs, although in this one i have to use arrays of pointers. thanks for the suggest though CommonTater
    No worries... I'll get out of the way and let the others chip in from here.... It's just that linked lists are such an elegant solution to the "unknown array size" problem.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    pNumber = (char *) calloc(size, sizeof(int));
    OK, so what is the data type of what pNumber points to? Is it an int? If it isn't an int, then why would you use the size of an int?

    Also, if you're compiling this as C, there is no need to cast the return value of malloc/calloc/realloc.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    so its the size of char?

    yea i read that the return is void, it doesn't care what is stored in memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 03:30 PM
  2. Pointers As 2D arrays
    By TieFighter in forum C Programming
    Replies: 29
    Last Post: 03-22-2010, 06:46 AM
  3. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  4. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  5. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM