Page 1 of 1
Assign a string to a variable using C...
Posted: Mon Mar 29, 2004 12:00 am
by pepito
Exists some way to assign a string value to a variable directly using C?
Some like this:
main() {
char my_name[100];
my_name = "Jose Antonio"; }
I know this is not possible using standar C, but maybe exist some way to emulate this kind of assignments.
Any idea?
Thank you!
pepito
RE:Assign a string to a variable using C...
Posted: Mon Mar 29, 2004 12:00 am
by hartyl
what you need is a string-copy-function. you have to copy a string char by char:
void strcpy(char* dest, char* src)
{ do
{ *dest=*src;
}while(*dest);
}
use it like this:
strcpy(my_name,"Jose Antonio");
greets, hartyl
RE:Assign a string to a variable using C...
Posted: Mon Mar 29, 2004 12:00 am
by pepito
Thank you,
What happend if I want to do some like this:
main() {
char my_name[100];
my_name = str_to_upper("Jose Antonio"); }
pepito
RE:Assign a string to a variable using C...
Posted: Mon Mar 29, 2004 12:00 am
by carbonBased
Use pointers:
char *name = (char *)"some string";
char *NAME = str_to_upper(name);
Jeff
RE:Assign a string to a variable using C...
Posted: Tue Mar 30, 2004 12:00 am
by common
The C standard ISO 9899:1999 specifically confronts this idea.
char *name = "blah";
char *blah = str_to_upper(name);
do not do this, or char *blah = str_to_upper("blah");
char *name = (char *)"blah"; is unnecessary, the behavior is the same, "blah" should be considered 'read-only.'
Use:
char name[100] = "blah"; instead
char *name2 = str_to_upper(name);
You may also use strcpy, strncpy, etc.
I know that in dealing with a number of environments here, char *blah = "blah"; may appear legal, but it technically is not.
RE:Assign a string to a variable using C...
Posted: Tue Mar 30, 2004 12:00 am
by pepito
O.K. I must use pointers, but...
My 'str_to_upper(char * string)' function must return a pointer to the 'string' parameter?
All my string functions must return a pointer to its 'string' parameter?
pepito
RE:Assign a string to a variable using C...
Posted: Tue Mar 30, 2004 12:00 am
by carbonBased
Your str_to_upper() should probably return a pointer to a newly allocated string, or I suppose it could return a pointer to the actual 'string' parameter, yes, but that's rather redundant.
Jeff
RE:Assign a string to a variable using C...
Posted: Tue Mar 30, 2004 12:00 am
by common
nope, not at all. Doesn't have to return anything or it can return an integer.
Example code:
(this returns a pointer)
char *str_to_upper(char *);
int somefunction()
{
char name[512] = "hello";
char *p;
p = str_to_upper(name);
/* name is now uppercase and p points to name */
return 0;
}
char *str_to_upper(char *s)
{
char *p = s;
while (*p)
{
if (*p >= 'a' && *p <= 'z')
*p -= 32;
++p;
}
return s;
}
Example #2 (doesn't return anything):
void str_to_upper(char *);
int main()
{
char name[512] = "hello";
char *p;
str_to_upper(name);
/* name is now uppercase and p points to name */
p = name; /* p now points to name */
return 0;
}
void str_to_upper(char *s)
{
while (*s)
{
if (*s >= 'a' && *s <= 'z')
*s -= 32;
++s;
}
}
Of the two, the second is actually more optimized, but isn't quite as "flexible"
Take your pick.
RE:Assign a string to a variable using C...
Posted: Wed Mar 31, 2004 12:00 am
by pepito
Thank you very much for every one!
pepito