[英]C programming passing a char array into a function
I am using a function to parse through userID, and paswd and some error checking. 我正在使用一个函数来解析userID,paswd和一些错误检查。 The function is called from my main()... However when executed only the first 4 characters of my UserID and Pswd are successfully extracted.
该函数从我的main()调用...但是当执行时,只能成功提取我的UserID和Pswd的前4个字符。 I am new to C-programming and coming from C# I am not sure where I am going wrong.
我是C编程的新手,来自C#我不知道我哪里出错了。 This should be fairly 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 point out, if I don't use the external function, and have all the code in my main func then it works. 另外,只是要指出,如果我不使用外部函数,并在我的主函数中包含所有代码,那么它的工作原理。
EDIT:- 编辑:-
Figured out the issue:- 找出问题: -
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 doesnt work quite as I expected it to.. it was reading the size of my pointer which is always 4 chars by default. sizeof并不像我预期的那样工作..它正在读取指针的大小,默认情况下总是4个字符。
I guess your pointer has a size of 4 Byte. 我猜你的指针大小为4 Byte。 therefore you only read 4 chars.
因此你只读了4个字符。
TL;DR TL; DR
The sizeof
isn't doing what you're expecting. sizeof
没有做你期望的事。 Try using strlen
instead. 尝试使用
strlen
代替。
You're only getting 4 characters copied because sizeof(char*[N])
for any N is just going to be the size of a pointer. 你只复制了4个字符因为任何N的
sizeof(char*[N])
只是指针的大小。 On your platform, a pointer must be 4 bytes (32 bits). 在您的平台上,指针必须是4个字节(32位)。
I think you actually mean to pass the base-address of the array into the function, but in that case your types aren't quite right. 我认为你的意思是将数组的基地址传递给函数,但在这种情况下你的类型并不完全正确。 Your compiler should be warning you about this.
您的编译器应该警告您这一点。 You should remove the
*
from your last 2 argument types: 您应该从最近的2个参数类型中删除
*
:
static void func1(int argc, char *argv[], char UserID[30], char Psw[30])
That should get rid of the warning, and it should actually make sizeof
behave correctly as well (since sizeof(char[30])
is 30). 这应该摆脱警告,它实际上应该使
sizeof
行为正确(因为sizeof(char[30])
是30)。 However, it's very easy to make mistakes with sizeof
since the behavior with a char*
and char[]
are different... I'd prefer using strlen
(or strnlen
if you want to avoid possible buffer overflows) here instead, which will simply tell you how many non-null characters you have. 然而,这是很容易做出配合失误
sizeof
因为与行为char*
和char[]
是不同的......我宁愿使用strlen
(或strnlen
如果你想避免可能的缓冲区溢出),而不是在这里,这将根本告诉你你有多少非空字符。
Using strnlen
rather than sizeof
would also help to tip you off that your parameter types are wrong, since it will complain that you're trying to pass a char**
to a function that expects a char*
. 使用
strnlen
而非sizeof
也将有助于提示你,你的参数类型是错误的,因为它会抱怨说,你想传递一个char**
给需要一个函数char*
。
Pass the array size 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 insure the destination arrays are null character terminated, suggest strncat()
--> "A terminating null character is always appended to the result." 为了确保目标数组是空字符终止,建议
strncat()
- >“终止空字符始终附加到结果。” strncpy()
has too many problems, it does not always result in an array with a null character. strncpy()
有太多问题,并不总是导致数组为空字符。
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);
}
[Edit] [编辑]
Corrected code - off by 1 更正代码 - 关闭1
Solution 解
#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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.