LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Test uninitialized variable (https://www.linuxquestions.org/questions/programming-9/test-uninitialized-variable-144905/)

AMMullan 02-11-2004 01:34 PM

Test uninitialized variable
 
Hey :)

I've gone completely blonde this morning... I'm trying to test if a variable has a value but can't get it for some reason...

Code:

if(strcmp(name, "")) {
    strcpy(name, getenv("USER"));
    printf("Using current username: %s\n", name);
  }

here name is a char variable, i just can't think of how to test if the variable has a value (tried if(name == NULL))... If I do a printf("%s\n", name) I get P??? ?...

jtshaw 02-11-2004 01:51 PM

When you declare a variable in C (this is C right?) it isn't initialized to anything in particular, it gets whatever happens to be in the memory space it was given. So rather then testing if it is uninitialized how bout making sure you initialize it to something on declaration? If it is a char * then set it equal to NULL (if you plan on dynamically allocating memory) or to an empty string of the correct size (if you are statically allocating memory).

AMMullan 02-11-2004 11:06 PM

Tried that but didn't work... This is what I've got so far:

Code:

int main(int argc, char *argv[]) {
  char name[64] = NULL, salt[32], password[64], newpwd[64];
  int menu_option;
  struct passwd *pwd;          /* Pointer to a structure in the pwd.h header file */
  struct spwd *shadow;          /* Pointer to a structure in the shadow.h header file */
 
  int optch;                    /* Current option charachter */
  char stropts[] = ":u:h";      /* Supported agruments */
 
  while((optch = getopt(argc, argv, stropts)) >= 0) {
    switch (optch) {
      case 'h':
        usage(argv[0]);
        return 0;
      case 'u':
        sprintf(name, "%s", optarg);
        break;
      case ':':
        fprintf(stderr, "No argument specified for -%c option\n", optopt);
        return 1;
      case '?':
        printf("Unknown option -%c.\n", optopt);
        printf("For help on usage please use: %s -h\n", argv[0]);
        return 1;
      }
  }
 
  if(name == NULL) {
    strcpy(name, getenv("USER"));
    printf("Using current username: %s\n", name);
  }

But when I run without parameters it doesn't ask me for a name, just uses nothing as a name and continues... and if i use the -u switch it uses the default system environment name...

Berng 02-11-2004 11:10 PM

AMMullan,
jtshaw's answer + you could use smth like this:

#define DEFUALT_LEN 1024
char* name=NULL;

int getUserName()
{
if(name==NULL)
name=(char*)calloc(DEFAULT_LEN,sizeof(char)); // alloc memory for name if not yet and set it to 0
if(name[0]==0) strncpy(name,getenv("USER"),DEFAULT_LEN); //check if name string has zero length and set it to env{USER} in this case
}
BestRegards,
Oleg.

AMMullan 02-11-2004 11:20 PM

:( Worked but then seg faulted when i used -u foo...

luxitan 02-12-2004 05:22 AM

hi, try to change this in your code:

Code:

char name[64] = {0}, salt[32], password[64], newpwd[64];
Code:

if(!strlen(name)) {
    strcpy(name, getenv("USER"));
    printf("Using current username: %s\n", name);
  }

this should work

AMMullan 02-12-2004 10:29 AM

God what an idiot I was - couldn't even think of the function (strlen) to use - luxitan your my hero :D

Thanks heaps everyone :)

*Goes back to the books*
:study:


All times are GMT -5. The time now is 07:34 PM.