+ 1
How can we take input elements in an array from the user without declaring size of an array in C Language ??
Answer should be in "C Language"...!!! [Edit : Thanks for Ur Answers😊...Plz do 1 more favour on me...Plz like this Question 🙏]
9 Antworten
+ 5
Stackoverflow Answer
array without an initial size is basically just a pointer. In order to dynamically set the size of the array, you need to use the malloc() or calloc() functions. These will allocate a specified amount of bytes of memory.
In your code above, declare temp as an int pointer
int *temp;
Then allocate space for it using malloc() or calloc(). The argument that these functions take is is the number of bytes of memory to allocate. In this case, you want enough space for d ints. So...
temp = malloc(d * sizeof(int));
malloc returns a pointer to the first byte in the block of memory that was just allocated. Regular arrays are simply pointers to the first byte in a sectioned off block of memory, which is exactly what temp is now. Thus, you can treat the temp pointer as an array! Like so:
temp[1] = 10;
int foo = temp[1];
printf("%d", foo);
+ 3
For better explanation u can visit here
https://stackoverflow.com/questions/17381867/declaring-arrays-in-c-language-without-initial-size/17381898#:~:text=Just%20create%20a%20pointer%2C%20and,it%20like%20as%20an%20array.&text=int%20*temp%20%3D%20null%3B%20%2F%2F,when%20done%20free(temp)%3B
+ 1
Thanx Martin Taylor 🙌
+ 1
Thanx HBhZ_C
+ 1
Thank you 😊 ♨️♨️
0
Not possible without pointers.Declare your pointer array and an integer to be the length for this array without initialisation.Without using malloc method you can not use array undefined size.Use malloc(sizeof(char)*size);
0
As far I know, we should mention array size at the time of array declaration in C..
0
Yeah I know that..That's why I have asked this question for getting solution Jayakrishna🇮🇳