I use the function for analysis through userID and paswd and some error checking. The function is called from my main () ... However, when executed, only the first 4 characters of my UserID and Pswd are successfully extracted. I am new to C programming and getting out of C #. I am not sure where I am going wrong. This should be pretty easy, can someone point me in the right direction?
static void func1(int argc, char *argv[], char *UserID[30], char *Psw[30]) { strncpy(UserID, argv[1], sizeof(UserID)); strncpy(Psw, argv[2], sizeof(Psw)); } int main(int argc, char *argv[]) { char UserID[30]; char Psw[30]; func1(argc, argv, UserID, Psw); }
Also, just to indicate if I don't use an external function and don't have all the code in my main func, then it works.
EDIT: -
Found out the problem: -
static void func1(int argc, char *argv[], char *UserID, char *Psw) { strncpy(UserID, argv[1], UserIDMaxSize); strncpy(Psw, argv[2], PswMaxSize); } int main(int argc, char *argv[]) { char UserID[UserIDMaxSize + 1]; char Psw[PswMaxSize + 1]; func1(argc, argv, UserID, Psw); }
sizeof doesn't work the way I expected it to ... it read the size of my pointer, which by default always has 4 characters.