Need to do something like this

userName="DeadUser" 

This value is assigned to a variable by a separate function.

  • If it is char (not char [] and not char * ), then in any way. Just one letter. In secret - sometimes you can drive into it as much as 4 (but you shouldn’t particularly indulge in it, purists will enter) - avp

1 answer 1

It depends on how the variable is declared. If it is declared as a pointer, then you can write

 char *userName = "DeadUser"; 

or

 char *userName; userName = "DeadUser"; 

or even

 #include <stdlib.h> #include <string.h> //... char *userName; userName = malloc( 9 * sizeof( char ) ); strcpy( userName, "DeadUser" ); //... free( userName ); 

If the variable is declared as an array, then you can write

 char userName[] = "DeadUser"; 

or

 #include <string.h> //... char userName[9]; strcpy( userName, "DeadUser" ); 
  • strdup() such a "secret weapon" like strdup() too early to trust him? - avp
  • 2
    @avp Actually, this is not a standard feature. This is a POSIX function. You can use it, but in order to compile the code, you need to either set a macro ad or a compiler option. - Vlad from Moscow
  • Well, you give. And since when is POSIX a standard? In general, if there is gnu, then you can use it. And if somewhere it (or some other) does not exist, then it is necessary to implement it yourself. - avp
  • one
    @avp POSIX - no decree to C standard. :) - Vlad from Moscow
  • POSIX is not a decree to the C standard. :) Oh, God ... And this: "POSIX (English portable operating system interface) is a set of standards that describe the interfaces between the operating system and the application program (system API), C library and a set of applications and their interfaces. "- means nothing? The standard in which LIBRARIES are stipulated With not a decree for LIBRARIES WITH?! - Sergey