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
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
Totally possible... Linked lists were made for just this purpose... an array without end.
How do i create an array with no end? ive tried leaving the [] empty but i get an error
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():Originally Posted by CommonTater
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.
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.
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
So why not show us what you've tried?
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.
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); }
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.
My next practice program is using structs, although in this one i have to use arrays of pointers. thanks for the suggest though CommonTater
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?Code:pNumber = (char *) calloc(size, sizeof(int));
Also, if you're compiling this as C, there is no need to cast the return value of malloc/calloc/realloc.
so its the size of char?
yea i read that the return is void, it doesn't care what is stored in memory.