2014-12-02 52 views
0

基本上,我使用strncpy截断字符,如果它大于字符数组大小。strncpy不能正确存储字符C

所以,我有以下变量和方法。

char studentName[6]; 
char colour[5]; 
char music[7]; 

strcpy(this->studentName, "null"); 
strcpy(this->colour, "null"); 
strcpy(this->music, "null"): 

void setName (char* studentName) 
{ 
strncpy(this->studentName, studentName, 6); 
this->studentName[6] = '\0'; // SET LAST TO NULL POINTER 
} 

void setColour (char* colour) 
{ 
strncpy(this->colour, colour, 5); 
this->colour[5] = '\0'; // SET LAST TO NULL POINTER 
} 

void setMusic (char* music) 
{ 
strncpy(this->music, music, 7); 
this->music[7] = '\0'; // SET LAST TO NULL POINTER 
} 

所以,如果我设置了学生的名字Jackson,它将截断至Jackso,但是,我的colour变量将是空白,我music变量将是null

另外,如果我尝试...

void setName (char* studentName) 
{ 
    strncpy(this->studentName, studentName, 6); 
    this->studentName[6-1] = '\0'; // SET LAST TO NULL POINTER 
} 

void setColour (char* colour) 
{ 
strncpy(this->colour, colour, 5); 
this->colour[5-1] = '\0'; // SET LAST TO NULL POINTER 
} 

void setMusic (char* music) 
{ 
strncpy(this->music, music, 7); 
this->music[7-1] = '\0'; // SET LAST TO NULL POINTER 
} 

,我设置了学生的名字Jackson我得到这样的Jacksonull。它将null添加到末尾

+2

当你声明'studentName [6]'时,有效指示是'[0]'到'[5]'。所以在位置'[6]'处设置''\ 0''是未定义的行为。 – abelenky 2014-12-02 22:29:14

+0

@abelenky,所以即使'this-> studentName [5] ='\ 0'',为什么它仍然会添加先前在这个数组中的null值, '杰克逊' – 2014-12-02 22:35:03

+0

既然你没有向我们展示过什么地方/何时/如何调用setName,setColour和setMusic函数,这很难说。发布更多代码,plz。 – abelenky 2014-12-02 22:36:16

回答

-1

尝试使用sizeof()而不是原始数字。它允许你改变数组的大小,而不用接触你的代码。

您首先复制sizeof(array)-1符号,然后设置索引为sizeof(array)-1(因为索引从0开始)到'\0'的最后一个符号。

您的代码编辑应该是这样的:

char studentName[6]; 
char colour[5]; 
char music[7]; 

strcpy(this->studentName, "null"); 
strcpy(this->colour, "null"); 
strcpy(this->music, "null"): 

void setName (char* studentName) 
{ 
strncpy(this->studentName, studentName, sizeof(this->studentName)-1); // Copy n-1 chars 
this->studentName[sizeof(this->studentName)-1] = '\0'; // n is set to null symbol 
} 

void setColour (char* colour) 
{ 
strncpy(this->colour, colour, sizeof(this->color)-1); 
this->colour[sizeof(this->color)-1] = '\0'; 
} 

void setMusic (char* music) 
{ 
strncpy(this->music, music, sizeof(this->music)-1); 
this->music[sizeof(this->music)-1] = '\0'; 
} 
+0

请不要调用''\ 0'是一个“空指针”,它只是整数0.这里的注释都是无用的和错误的。 – 2014-12-02 22:56:43

+0

“NULL POINTER”应该准备好“空字节”(它不是指针) – 2014-12-02 22:57:10

+0

'sizeof'表达式需要是'this-> studentName'等。 – 2014-12-02 22:57:33

1

这里(假设Linux平台上你可能有,如果你在Windows,而不是一定要取是strlcpy执行。)

#include <bsd/string.h> 

char studentName[6]; /* Good to know: Globals are initialized to zero */ 
char colour[5]; 
char music[7]; 

/* Pretty useless function since a single strlcpy call is enough */ 
static size_t setX(char *buf, size_t buflen, const char *new_val) { 
    return strlcpy(buf, new_val, buflen); 
} 

int main(int argc, char **argv) { 
    setX(studentName, 6, "Peter"); /* Please read man page and check return value */ 
} 

现在strlcpy只要长度参数> 0,就保证NUL终止。

+0

@chux Yep错过了,thx。 – Jite 2014-12-03 09:29:24