Is it possible to declare a multiple character containing elements in a char array in C? I am not getting into strings right now. Thanks in advance.
Multi-Char Array in CAdd multiple characters to character elements
Page 1 of 1
4 Replies - 3987 Views - Last Post: 07 September 2008 - 08:19 AM
Replies To: Multi-Char Array in C
#2
Re: Multi-Char Array in C
Posted 07 September 2008 - 07:58 AM
Yes, try something like this:
char* varName = "test";
#4
Re: Multi-Char Array in C
Posted 07 September 2008 - 08:18 AM
Yup.
<edit>
Well, I guess it is still counted as a string when you are outputting it so, maybe it is counted as a string for normal, but it works with setting multiple characters to a single variable.
Here is some better code (for an actual array):
</edit>
<edit2>
NOTE - You can treat either as an array like so:
Which will print out "h" for my first example and "e" for the second.
</edit2>
Hope that helps.
<edit>
Well, I guess it is still counted as a string when you are outputting it so, maybe it is counted as a string for normal, but it works with setting multiple characters to a single variable.
Here is some better code (for an actual array):
char test[] = {'t', 'e', 's', 't'};
</edit>
<edit2>
NOTE - You can treat either as an array like so:
printf("This letter: %c", test[1]);
Which will print out "h" for my first example and "e" for the second.
</edit2>
Hope that helps.
This post has been edited by BetaWar: 07 September 2008 - 08:22 AM
#5
Re: Multi-Char Array in C
Posted 07 September 2008 - 08:19 AM
It's a pointer to a null terminated string.
* isn't just an asterisk, it means "pointer." Pointers... well, they point to a certain position in the memory.
One thing you might wanna do is delimit it with a \0 at the end.
Otherwise, when you come to use it, it could be adding other crap stored in the memory after that.
Example:
Imagine the user's memory being stored as a string. When you come to use that string, it will start from the beginning, and continue until a \0 is found (NULL terminating character)
It could look like this:
testwzks*2j\0
In which case, myStr will actually be "testwzks*2j"
If you add a \0 to the end of your string, it will be test\0twzks*2j\0 in the memory. Effectively, myStr will then be "test"
I don't think there are many times when this actually occurs, since (AFAIK) many compilers will add \0 to the end of it for you. It's just worth typing that extra \0 to be on the safe side.
Hope this helps
[I was kinda rambling a bit, I forgot what I was gonna say about 4 times]
* isn't just an asterisk, it means "pointer." Pointers... well, they point to a certain position in the memory.
One thing you might wanna do is delimit it with a \0 at the end.
Otherwise, when you come to use it, it could be adding other crap stored in the memory after that.
Example:
Imagine the user's memory being stored as a string. When you come to use that string, it will start from the beginning, and continue until a \0 is found (NULL terminating character)
It could look like this:
testwzks*2j\0
In which case, myStr will actually be "testwzks*2j"
If you add a \0 to the end of your string, it will be test\0twzks*2j\0 in the memory. Effectively, myStr will then be "test"
I don't think there are many times when this actually occurs, since (AFAIK) many compilers will add \0 to the end of it for you. It's just worth typing that extra \0 to be on the safe side.
Hope this helps
[I was kinda rambling a bit, I forgot what I was gonna say about 4 times]
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|