0

Given this text:

include

include

int main (void) { string text = get_string("Text: "); char num[] = text; }

When this is run, it gives the error as stated in the heading of this question I am trying to assign a char to each array, (ex: num1 = h, num[2] = e, and so on, when the user wrote "hello" in text) Please help (Picture is here, in case what i wrote is not shown properly)

-1

Yo're getting two errors here. The first one is because you're trying to assign something to a char array without first specify its size i.e. char num[30]; . The second is because you're trying to assign a string to a char array. For the compiler they are diferents things. You can copy a string to a string and you can copy a char to a char, but to copy a string or a char array to another char array you have to go through it i.e. num[0] = text[0]; num[1] = text[1] and so on. You can do that in a while loop: string text = get_string("Text: "); char num[20]; int i = 0; while (text[i] != '\0') { num[i] = text[i]; i++; } printf("%s\n", num); In the future you'll see that there are some tools that do this for you but it's good to know how it works

3
  • Great! it worked! Thanks Tritum – ThisIsMe Apr 14 '20 at 5:19
  • Sorry...I was trying to press upvote, but i accidentally pressed the other one... and the system won't allow me to change it... – ThisIsMe Apr 14 '20 at 5:24
  • Don't worry No problem – Tritum Apr 15 '20 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .