Assign a string to a variable using C...

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pepito

Assign a string to a variable using C...

Post 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
hartyl

RE:Assign a string to a variable using C...

Post 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
pepito

RE:Assign a string to a variable using C...

Post 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
carbonBased

RE:Assign a string to a variable using C...

Post by carbonBased »

Use pointers:

char *name = (char *)"some string";
char *NAME = str_to_upper(name);

Jeff
common

RE:Assign a string to a variable using C...

Post 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.
pepito

RE:Assign a string to a variable using C...

Post 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
carbonBased

RE:Assign a string to a variable using C...

Post 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
common

RE:Assign a string to a variable using C...

Post 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.
pepito

RE:Assign a string to a variable using C...

Post by pepito »

Thank you very much for every one!

pepito
Post Reply