strcpy

string, CString, char[]与ASCII的字符表示

情到浓时终转凉″ 提交于 2019-12-14 23:33:31
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 对于字符串的处理在C++中可谓是一个颇为棘手的问题,而像JAVA和C#这种基于托管的平台则不存在此类问题。 我们先来讨论一下memcpy和strcpy这两个方法。 void* memcpy(void *memTo, const void *memFrom, size_t size); char* strcpy(char * dest, const char * src); 这两个方法的区别主要有一下3个: 1. 复制的内容不同,strcpy只能复制字符串,而memcpy则可以复制任何的内容,例如char[],int,struct,class等。 2. 复制的方法不同,strcpy不需要指定要复制的长度,当遇到在src字符串中的“\0”(空字符)时才停止复制,因此很容易出现溢出的现象。而memcpy则是根据其第三个参数决定要复制的长度的,避免了此类问题。 3. 用途不同,通常在复制字符串时用strcpy,而在复制其他类型的数据时则一般采用memcpy。 4. 若要复制ASCII为1的SOH,在memcpy中要用\0表示,如果直接输入0,则代表0这个字符。 需要注意的是: 在发送与设备之间通讯的命令的情况下,很多时候命令会包含空字符null,这种时候就要慎用strcpy了

Why does a char array need strcpy and char star doesn't - using structs in C

こ雲淡風輕ζ 提交于 2019-12-13 17:11:43
问题 I have a misunderstanding regarding this code - typedef struct _EXP{ int x; char* name; char lastName[40]; }XMP ...main... XMP a; a.name = "eaaa"; a.lastName = strcpy(a.lastName, "bbb"); Why can't I use: a.lastName = "bbbb"; and that's all? 回答1: Well consider the types here. The array has the contents of the string, while the char* merely points to the data. Consequently the array requires strcpy and friends. Besides, if you allocated memory for the char* on the heap or stack and then wanted

strcpy()/strncpy() crashes on structure member with extra space when optimization is turned on on Unix?

纵饮孤独 提交于 2019-12-13 11:36:27
问题 When writing a project, I ran into a strange issue. This is the minimal code I managed to write to recreate the issue. I am intentionally storing an actual string in the place of something else, with enough space allocated. // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <stddef.h> // For offsetof() typedef struct _pack{ // The type of `c` doesn't matter as long as it's inside of a struct. int64_t c; } pack; int main(){ pack *p; char str[9] =

Copy contents of non null terminated char array into another char array

好久不见. 提交于 2019-12-13 11:15:51
问题 I have an array of structs, each struct has a char array and an int. typedef struct { int id; //Each struct has an id char input[80]; //Each struct has a char array } inpstruct; inpstruct history[10]; //Array of structs is created I have another char array that contains the user's input char inputBuffer[80]; The user enters a word followed by the \n character. For example, inputBuffer will contain 3 chars: 'l' 's' '\n' . I want to copy all the chars in inputBuffer into history[index].input I

Segmentation fault while using strcpy()?

ぃ、小莉子 提交于 2019-12-13 06:42:40
问题 I have a global structure: struct thread_data{ char *incall[10]; int syscall arg_no; int client_socket; }; and in main() char buffer[256]; char *incall[10]; struct thread_data arg_to_thread; strcpy(incall[0],buffer); /*works fine*/ strcpy(arg_to_thread.incall[0],buffer); /*causes segmentation fault*/ Why does this happen and Please suggest a way out. thanks 回答1: A segfault means that something is wrong. But no segfault does not mean that something isn't wrong. If two situations are basically

Copy results of strtok to 2 strings in C

纵然是瞬间 提交于 2019-12-12 23:23:11
问题 Ok, so I have the code char *token; char *delimiter = " "; token = strtok(command, delimiter); strcpy(command, token); token = strtok(NULL, delimiter); strcpy(arguments, token); and it gives me EXC_BAD_ACCESS when i run it, and yes, command and arguments are already defined. 回答1: Why are you copying the token into command when you're parsing command ? It's a very unsafe thing to do. You can do: char *command_tok, *args_tok; command_tok = strtok(command, delimiter); args_tok = strtok(NULL,

strcpy pass initialized null pointer c [duplicate]

江枫思渺然 提交于 2019-12-12 19:13:25
问题 This question already has answers here : Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer (5 answers) Closed 3 years ago . I'm trying out the following code: int main() { char *yo = "yo"; char *whaddup = NULL; strcpy(whaddup,yo); } and I get a segfault. Complete C noob here - other places say I should initialize whaddup as an array. Why can't I pass in a pointer to null? 回答1: Just any strcpy documentation will tell you that the destination string

Comparing two strings, problems with strcmp

浪尽此生 提交于 2019-12-12 15:53:21
问题 I'm trying to check if the line read from stdin begins with "login:" but strcmp does not seem to work. char s1[20], s2[20]; fgets(s1, 20, stdin); strncpy(s2,s1,6); strcmp(s2, "login:"); if( strcmp(s2, "login:") == 0) printf("s2 = \"login:\"\n"); else printf("s2 != \"login:\"\n"); I don't care what comes after "login:", i just want to make sure that's how the command is given. What am i doing wrong? 回答1: strcmp returns 0 if the two strings are exactly the same to accomplish what you want to do

Having trouble adding an int to a string, tried using sprintf but I'm having trouble

血红的双手。 提交于 2019-12-12 04:53:01
问题 I am trying to read a file and print all of the words that are in the file, ignoring all other spaces and symbols. I have it working with strcpy but it's giving me an error and I'm trying to use sprintf but I don't really understand how that function words. It's printing random integers instead of the strings. Edit: I'm completely new to C so I don't have my pointers down too well. FILE *file; file = fopen("sample_dict.txt", "r"); int c; int wordcount = 0; int count = 0; const char *a[10];

Making strcpy function with linked list in c

為{幸葍}努か 提交于 2019-12-12 04:36:19
问题 I was making my own strcpy function using linked list but couldn't get how to do. Without using linked list it could be like this char* cp2014strcpy(char * dest_ptr, const char * src_ptr) { char* strresult = dest_ptr; if((NULL != dest_ptr) && (NULL != src_ptr)) { while (NULL != src_ptr) { *dest_ptr++ = *src_ptr++; } *dest_ptr = NULL; } return strresult; } but I couldn't get how to make strcpy using linked list. 回答1: #include <stdio.h> #include <stdlib.h> typedef struct node { char ch; struct