LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2004, 01:34 PM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
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??? ?...
 
Old 02-11-2004, 01:51 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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).

Last edited by jtshaw; 02-11-2004 at 01:52 PM.
 
Old 02-11-2004, 11:06 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
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...
 
Old 02-11-2004, 11:10 PM   #4
Berng
Member
 
Registered: Dec 2003
Location: Siberia,Russia
Distribution: Basically RedHat
Posts: 34

Rep: Reputation: 15
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.

Last edited by Berng; 02-11-2004 at 11:16 PM.
 
Old 02-11-2004, 11:20 PM   #5
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Worked but then seg faulted when i used -u foo...
 
Old 02-12-2004, 05:22 AM   #6
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
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
 
Old 02-12-2004, 10:29 AM   #7
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

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

Thanks heaps everyone

*Goes back to the books*
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to test if an env variable is set King of Men Linux - Newbie 5 11-03-2005 04:39 AM
test a variable to see if it contains information farmerjoe Programming 4 04-25-2005 02:34 PM
FC4 Test 1 CD Test failes every DISC Jimbo99 Linux - Software 1 03-18-2005 03:16 PM
R question- how to use apply on a two variable t-test SerfurJ Programming 0 12-30-2003 02:34 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration