Here is how I defined and assigned a string array at the same time
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main()
{
string word = get_string("Enter a Word;");
for(int i = 0; i<strlen(word); i )
{
printf("%c\n",word[i]);
}
}
Now I want to apply the same technique for taking integer values from user
#include <stdio.h>
#include <cs50.h>
int main()
{
int scores[] = get_int("Enter score:");
int len = sizeof(scores)/sizeof(int);
for(int i = 0; i<len; i )
{
printf("%i\n", scores[i]);
}
}
But the code doesn't get compiled and compiler says like this:
IntArray.c:5:9: error: array initializer must be an initializer list or wide string literal
int scores[] = get_int("Enter integer:");
^
1 error generated.
make: *** [<builtin>: IntArray] Error 1
I know we have a for loop for that solution, which goes like this: That's why I have to keep using for loop, which looks like this:
#include <stdio.h>
#include <cs50.h>
int main()
{
int scores[3];
for(int I = 0; I<3; I )
{
score[I] = get_int("Enter score:);
}
for(int j =0; j<3; j )
{
printf("%I\n", scores[j]);
}
}
My question is can I define and assign the integer array at the same time?
I tried to define and assign an integer array at the same time as we do with the single string, but I couldn't.
CodePudding user response:
Building on this earlier answer. The function read_ints reads integers until it reads end_of_list, after which it returns.
void read_ints(Array *a, int end_of_list)
{
int current;
while(1){
scanf("%d",¤t);
if(current == end_of_list) return;
insertArray(a,current);
}
}
int main()
{
Array a;
initArray(&a, 5);
printf("Enter numbers:");
read_ints(&a,-1);
for(int i=0; i<a.used; i ) printf("%d ", a.array[i]);
return 0;
}
CodePudding user response:
Can I define and assign an integer array at the same time like we do with the string array?
string word
is not an array. That object is of a CS50 type string
, which is a pointer, that points to an array of char
.
Arrays are not pointers.
Pointers are not arrays.
Code can create similar code to define an int
pointer ...
// string word = get_string("Enter a Word;");
int *ints = get_ints("Enter ints;");
... and use a special value, like INT_MIN
, to denote the end of the array of int
s. Of course then INT_MIN
is not available as a value to read. This is analogous to C strings, which uses a special value, the null character to signify the end of the array.
My question is can I define and assign the integer array at the same time?
Yes.
See @M.M comment.
int scores[] = { get_int("Enter score:"), get_int("Enter score:"), get_int("Enter score:") };
This obliges that the array size is determined before getting the data.
Code could read the int
s into some collection of data, then use the int
count to define a variable length array (VLA). But VLAs may not be initialized. They could be assigned later.
some_tbd_data_collection_type data = 0;
unsigned n = get_ints(&data);
int a[n];
for (unsigned i = 0; i < n; i ) {
a[i] = get_int(&data, i);
}
data_free(&data);