View previous topic :: View next topic |
Author |
Message |
rfjhh
Joined: 31 Mar 2011 Posts: 51 Location: Mexico
|
Strings |
Posted: Thu Mar 31, 2011 6:15 pm |
|
|
In PIC C, how can I use string type variables?
In c# I can define
string a;
.
.
.
a = "example";
.
.
. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Re: Strings |
Posted: Thu Mar 31, 2011 9:28 pm |
|
|
rfjhh wrote: | In PIC C, how can I use string type variables?
In c# I can define
string a;
.
.
.
a = "example";
.
.
. |
I would also strongly recommend getting "The C Programming Language" by Kernighan & Ritchie
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
eliasS
Joined: 22 Jun 2010 Posts: 1 Location: Culiacan,Mexico
|
|
Posted: Thu May 05, 2011 11:35 am |
|
|
In the old C a string basically is an array of chars, if you want to declare a string variable you don't use string="dummy"; instead you use a char array and you can't initialize it like this char myString="dummy"; This will create an array of chars of 6 elements 'd' will be at this location myString[0] and 'y' in myString[5] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu May 05, 2011 2:55 pm |
|
|
99% right. The 'y' is at myString[4]. myString[5] contains the 'null terminator' '\0' character.
This is the only thing 'special' about strings. In C, a string is 'a null terminated array of characters. So a 5 character string, needs 6 characters of storage, the extra one for the terminator character.
Remember also, that since a 'string' is not a type in C, it has to be explicitly copied, either 'character by character', by looping through the array, or by using the string functions (which do this for you).
Best Wishes |
|
|
rfjhh
Joined: 31 Mar 2011 Posts: 51 Location: Mexico
|
Thanks |
Posted: Thu May 05, 2011 7:12 pm |
|
|
I've got the answer... thank you |
|
|
|