Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,267 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,195 people online right now. Registration is fast and FREE... Join Now!




Multi-Char Array in C

 
Reply to this topicStart new topic

Multi-Char Array in C, Add multiple characters to character elements

usamamuneeb
6 Sep, 2008 - 11:10 PM
Post #1

D.I.C Head
**

Joined: 4 Sep, 2008
Posts: 104


My Contributions
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.

User is offlineProfile CardPM
+Quote Post


BetaWar
RE: Multi-Char Array In C
7 Sep, 2008 - 06:58 AM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 3,811



Thanked: 203 times
Dream Kudos: 1325
My Contributions
Yes, try something like this:

CODE
char* varName = "test";

User is online!Profile CardPM
+Quote Post

usamamuneeb
RE: Multi-Char Array In C
7 Sep, 2008 - 07:11 AM
Post #3

D.I.C Head
**

Joined: 4 Sep, 2008
Posts: 104


My Contributions
Just an asterisk?
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Multi-Char Array In C
7 Sep, 2008 - 07:18 AM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 3,811



Thanked: 203 times
Dream Kudos: 1325
My Contributions
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):

CODE

char test[] = {'t', 'e', 's', 't'};

</edit>

<edit2>
NOTE - You can treat either as an array like so:

CODE
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: 7 Sep, 2008 - 07:22 AM
User is online!Profile CardPM
+Quote Post

gabehabe
RE: Multi-Char Array In C
7 Sep, 2008 - 07:19 AM
Post #5

i > u
Group Icon

Joined: 6 Feb, 2008
Posts: 7,381



Thanked: 154 times
Dream Kudos: 2850
Expert In: Lots of things.

My Contributions
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 smile.gif

[I was kinda rambling a bit, I forgot what I was gonna say about 4 times]
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 12:47PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month