Multi-Char Array in C

Add multiple characters to character elements

Page 1 of 1

4 Replies - 5046 Views - Last Post: 07 September 2008 - 08:19 AM Rate Topic: -----

#1 trotski  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 04-September 08

Multi-Char Array in C

Post icon  Posted 07 September 2008 - 12:10 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Multi-Char Array in C

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1107
  • View blog
  • Posts: 6,924
  • Joined: 07-September 06

Re: Multi-Char Array in C

Posted 07 September 2008 - 07:58 AM

Yes, try something like this:

char* varName = "test";

Was This Post Helpful? 0
  • +
  • -

#3 trotski  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 04-September 08

Re: Multi-Char Array in C

Posted 07 September 2008 - 08:11 AM

Just an asterisk?
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1107
  • View blog
  • Posts: 6,924
  • Joined: 07-September 06

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):

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

Was This Post Helpful? 0
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

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]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1