C programming passing a char array to a function

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]; /* max val defined in a header file */ char Psw[PswMaxSize + 1]; /* max val defined in a header file */ 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.

+5
source share
4 answers

Pass the size of the array to the function

 static void func1(int argc, char *argv[], char *UserID, size_t UserIDSize, char *Psw, size_t PswSize) { if (argc> 1) strncpy(UserID, argv[1], UserIDSize); if (argc> 2) strncpy(Psw, argv[2], PswSize); } int main(int argc, char *argv[]) { char UserID[30] = {0}; char Psw[30] = {0}; func1(argc, argv, UserID, sizeof UserID, Psw, sizeof Psw); } 

To ensure that the target arrays are terminated with a null character, suggest strncat() → "The final null character is always appended to the result." strncpy() has too many problems, this does not always result in an array with a null character.

 static void func1(int argc, char *argv[], char *UserID, size_t UserIDSize, char *Psw, size_t PswSize) { UserId[0] = '\0'; // if (argc> 1) strncat(UserID, argv[1], UserIDSize); if (argc> 1) strncat(UserID, argv[1], UserIDSize - 1); Psw[0] = '\0'; // if (argc> 2) strncat(Psw, argv[2], PswSize); if (argc> 2) strncat(Psw, argv[2], PswSize - 1); } 

[change]

Corrected code - off for 1

+1
source

I assume your pointer is 4 bytes in size. therefore you read only 4 characters.

+2
source

TL DR

sizeof does not do what you expect. Use strlen instead.


You get only 4 characters, because sizeof(char*[N]) for any N will only be the size of the pointer. On your platform, the pointer should be 4 bytes (32 bits).

I think you really want to pass the base address of the array to a function, but in this case your types are not entirely correct. Your compiler should warn you about this. You must remove * from the last two types of arguments:

 static void func1(int argc, char *argv[], char UserID[30], char Psw[30]) 

This should get rid of the warning, and in fact, it should behave correctly sizeof (since sizeof(char[30]) is 30). However, it is very easy to make mistakes with sizeof , since the behavior with char* and char[] is different ... I would prefer to use strlen (or strnlen if you want to avoid possible buffer overruns) instead, which just tells you how many unnecessary characters you have.

Using strnlen rather than sizeof will also help you to protest that your parameter types are incorrect, as it will complain that you are trying to pass char** function expecting char* .

+1
source

Decision

 #include <stdio.h> #include <string.h> void func1(int argc, char *argv[], char *UserID, char *Psw) { strncpy(UserID, argv[1], strlen(argv[1])); strncpy(Psw, argv[2], strlen(argv[2])); printf("DATA: %s \n",UserID); printf("DATA1: %s \n",Psw); } int main(int argc, char *argv[]) { char UserID[30]; char Psw[30]; printf("argv1 %ld \n",strlen(argv[1])); printf("argv2 %ld \n",strlen(argv[2])); func1(argc, argv, UserID, Psw); } 
-1
source

Source: https://habr.com/ru/post/1246899/


All Articles