Tag: strncpy

我的输出有一些奇怪的符号显示

我不得不为我的课程编码。 编码是关于要求用户键入他们的姓名,年龄和身份。 然后程序应该根据他们名字中的前6个字母,他们的年龄和他们学生ID中的前两个字母的密码。 问题是输出中有一个未识别的符号(╠╠╠╠╠╠╠╠╠╠╠╠╠╠)。 谁能告诉我它为什么存在>? 然后它应该计算并显示密码的长度。 这是代码: #include #include void main(void) { char name[20], id[9], age[3], passcode[10]; int x; puts(“Enter your name:\n”); gets(name); puts(“\nEnter your student id:\n”); gets(id); puts(“\nEnter your age:\n”); gets(age); x = strlen(passcode); strncpy(passcode, name, 6); strncat(passcode, id, 2); printf(“The passcode is:%s \n”,passcode); printf(“The passcode has %d characters..\n”,x); } 它看起来像: Enter your name: […]

C:创建自己的strncpy版本

我正在创建自己的strncpy版本。 我的代码似乎接受输入正常,但程序在输入后终止。 strncpy似乎也用空值填充复制的函数,如果它比第一个短 – 那是什么意思,我如何在我的代码中实现它? #include #include #define SIZE 50 #define STOP “quit” char *copywords(char *str1, char *str2, int n); int main(void) { char words[SIZE]; char newwords[SIZE]; int num; int i = 0; int j = 0; printf(“Type a word, and the # of chars to copy, or type ‘quit’ to quit: “); fgets(words, SIZE, […]

在C中更有效地使用strncpy复制n个字符

我想知道考虑到最大量的字符,是否有更干净,更有效的方法来执行以下strncpy 。 我觉得自己太过分了。 int main(void) { char *string = “hello world foo!”; int max = 5; char *str = malloc (max + 1); if (str == NULL) return 1; if (string) { int len = strlen (string); if (len > max) { strncpy (str, string, max); str[max] = ‘\0’; } else { strncpy (str, string, […]

memcpy()和strncpy()有什么区别,因为后者很容易替代前者?

memcpy()和strncpy()之间有什么重大区别? 我问这个是因为我们可以轻松地改变strncpy()来复制我们想要的任何类型的数据,而不仅仅是字符,只需将前两个非char*参数转换为char*并将第三个参数更改为大小的倍数即可。非char类型。 在下面的程序中,我已成功使用它将整数数组的一部分复制到其他程序中,并且它与memcpy()一样好用。 #include #include int main () { int arr[2]={20,30},brr[2]={33,44}; //memcpy(arr,brr,sizeof(int)*1); strncpy((char*)arr,(char*)brr,sizeof(int)*1); printf(“%d,%d”,arr[0],arr[1]); } 同样,我们可以使它适用于float或其他数据类型。 那么与memcpy()的显着区别是什么? PS:另外,我想知道为什么memcpy()在string.h头文件中,因为几乎所有的库函数都与字符串相关,而memcpy()本质上更通用。 任何原因?

strncpy()的最佳替代方法是什么?

函数strncpy()并不总是null终止所以我想知道什么是始终为null终止的最佳替代? 我想要一个函数,如果: strlen(src) >= n /*n is the number of characters to be copied from source*/ 没有必要像这样添加更多代码: buf[sizeof(buf)-1] = 0;

‘strncpy’与’sprintf’

我可以在我的应用程序中看到许多用于复制字符串的sprintf 。 我有一个字符数组: char myarray[10]; const char *str = “mystring”; 现在,如果我想将字符串str复制到myarray ,最好使用: sprintf(myarray, “%s”, str); 要么 strncpy(myarray, str, 8); ?

strcpy()/ uninitialized char指针这个代码背后的诀窍是什么?

#include #include #include void main () { char *imsi; unsigned int i; int val; char *dest; imsi = “405750111”; strncpy(dest,imsi,5); printf(“%s”,dest); /* i = 10; */ } 在上面的代码中,如上所述对i = 10赋值进行了注释,代码工作正常,没有错误。 当包含赋值用于编译时,错误(分段错误)发生在strncpy(dest,imsi,5); 。 通过避免对变量i的优化(即, volatile int i; ),即使包括赋值( i = 10 ),也清除错误。

遍历C字符串:获取字符串的最后一个单词

你怎么会得到一个字符串的最后一个字,从’\ 0’换行符到最右边的空格? 例如,我可以在这样的地方为str分配一个字符串: char str[80]; str = “my cat is yellow”; 我怎么会变黄?

strncpy并不总是空终止

我使用下面的代码: char filename[ 255 ]; strncpy( filename, getenv( “HOME” ), 235 ); strncat( filename, “/.config/stationlist.xml”, 255 ); 收到此消息: (warning) Dangerous usage of strncat – 3rd parameter is the maximum number of characters to append. (error) Dangerous usage of ‘filename’ (strncpy doesn’t always null-terminate it).

strncpy文档问题

关于strncpy : http : strncpy ,它提到了以下内容: 没有空字符隐式附加到目标的末尾,因此如果源中的C字符串的长度小于num,则目标将仅以空值终止。 这句话是什么意思?