what is teh difference between int line[10];
and char line[10]; I know they are arrays but when do I use int and char?
what is teh difference between int line[10];
and char line[10]; I know they are arrays but when do I use int and char?
I prefer to use int for numeric values and char for characters. For example, if my array were to hold this set:
{ 1, 2, 15, 87, 45, 18, 32 }
I would use int for my array even though char can hold each of those values adequately. If, however, my array were to hold this set:
{ 'a', 't', 'g', 'f', 'p', 'r' }
I would use a char array even though each of those values could be held by an int and I'm not terminating the array with '\0' to make a string.
-Prelude
My best code is written with the delete key.
>and what if I have a mix of digits and char....
If the digits cannot be represented as character literals then you have a bad design:
{ 123, 'b', 34, 'Q' } /* Yucky */
-Prelude
My best code is written with the delete key.
I meant what I f I want to scan a line from text and put it in an array. That line can have anything....
Or would I then not use an array and use something else like malloc
>I meant what I f I want to scan a line from text and put it in an array.
This is a common operation, read the line as a character string and then parse it with either sscanf or other string conversion functions like strtol and strtod.
-Prelude
My best code is written with the delete key.
what does parse it mean?
By the way thanks for the clarification. I really appreciated
>what does parse it mean?
It basically means that you break the string up into independent and manageable pieces that you can then convert however you need. A simple example would be if the lines were always like this:
int int double string
You could parse the string like so:
Granted, this is a rather ugly and contrived example, but you get the idea.Code:int one, two; double three; char four[SIZE]; char s[SIZE]; fgets ( s, sizeof s, stdin ); sscanf ( s, "%d%d%lf%s", one, two, three, four );
-Prelude
My best code is written with the delete key.