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, "")) { |
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).
|
Tried that but didn't work... This is what I've got so far:
Code:
int main(int argc, char *argv[]) { |
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. |
:( Worked but then seg faulted when i used -u foo...
|
hi, try to change this in your code:
Code:
char name[64] = {0}, salt[32], password[64], newpwd[64]; Code:
if(!strlen(name)) { |
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. |