strcat

C风格字符串string.h

你离开我真会死。 提交于 2020-01-18 18:56:43
C语言本身没有字符串的标识符, C语言的字符串定义有两种方式,char*与char str[] C语言识别存储字符串,是靠数组的结构,单个字符储存, 并在末尾处自动加上 '\0’作为字符串结束的标识。 char*定义的字符串 char* 定义的变量存储在 字符常量区 ,定义的是一个常量。所以char* 一旦定义的就无法修改内部的单个字符。所以通常使用 const char* char str[]定义的字符串 char str[] 的变量存储在 栈上 ,所以可以通过下标来修改字符串中某个字符。 char * cstr = "string" ; printf ( "%c" , cstr [ 0 ] ) ; //输出 s printf ( "%c" , cstr + 1 ) ; //输出 t //char* 可以通过下标访问单个字符 cstr [ 0 ] = 'S' ; // X //但若是要修改其中的内容,编译就会报错 char str [ ] = "string" ; str [ 0 ] = 'S' ; printf ( "%c" , str [ 0 ] ) ; //输出 S //数组方式创建 既可以修改单个字符,也可以单个字符访问 strlen()求字符串长度 通过遍历字符串中的字符,遇到'\0'时停止遍历,此时就能得出字符串的长度。 char * str = "string" ;

C- Character Push and Pop operation [closed]

泪湿孤枕 提交于 2020-01-17 12:49:10
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 months ago . Stack create(int c) { Stack S=(Stack)malloc(sizeof(struct stack)); S->size=c; S->top=-1; S->array=(char *)malloc(sizeof(char)*c); return S; } Stack makeEmpty(void) { Stack *S1=create(100); S1[0]->top=-1; return S1; } char pop(Stack S) { return S->array[S->top--]; }; int main(void) { Stack *S1; S1=makeEmpty();

strcat segmentation fault

ぐ巨炮叔叔 提交于 2020-01-11 11:53:07
问题 The second call to strcat here is generating a segmentation fault, why? #include <unistd.h> #include<stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <pthread.h> int main (int argc, char * argv[]) { char command[250]; //if(scanf("%199s", command) == 1) gets(command); puts(command); int pipeIntId; char whitespaceseparator[2]=" "; char pidstring [30]; int pid= getpid(); sprintf(pidstring,"%d", pid); char * whitespace_and

strcpy, strdup, strcat, strncpy, strndup

本小妞迷上赌 提交于 2020-01-10 12:32:26
http://hi.baidu.com/liuhuman/item/c862c932b272d020b3c0c532 char* strcpy (char* dst, const char* src); //如果dst的长度 小于或者等于 strlen(src)时, src多余的字符串仍然被复制,将覆盖原先存储于数组后面的内存空间的值。 char* strdup(const char* src); //这个函数包含了malloc和strcpy, 不用担心在strcpy中dst的长度问题 char* strcat(char* dst, cosnt char* src); // 需要保证dst的大小足至少是strlen(dst) + strlen(src) + 1,否则数组溢出。 char* strncpy(char* dst, const char* src, size_t len); // 它总是正好向dst写入len个字符。 如果strlen(src) 小于 len , dst数组就用额外的'\0'填充到len个长度。 如果strlen(src) 大于或者等于 len, 那么只有len个字符被复制到dst中。注意: 它的结果将不会以'\0'结束 char* strndup(const char* src, size_t len); //复制len个字符串,它比strcpy好在:

Segmentation fault using strcat

我与影子孤独终老i 提交于 2020-01-09 05:43:04
问题 Here is my code : char *name, name_log="log-"; ------getting 'name' from user----- strcat(name_log, name); char ext[] = ".log"; strcat(name_log, ext); What i need to end up with is name_log = "log-'name'.log" but Im getting a segmentation fault error :((. What am I doing wrong and how can I fix it ? Thx 回答1: For a start, if this is your code: char *name, name_log="log-"; then name_log is a char, not a char pointer. Assuming that's a typo, you cannot append to string literals like that.

Safe way to concat two strings in C

爷,独闯天下 提交于 2020-01-07 09:46:11
问题 I have the following code that concats two strings: char *getConcatString(char *str1, char *str2) { char *finalString = malloc(1 + strlen(str1) + strlen(str2)); // Needs to be freed by the user after use if(finalString == NULL) return NULL; strcpy(finalString, str1); strcat(finalString, str2); return finalString; } Is there a more safe way to do this? Like for ex. strncat and strncpy? Thanks 回答1: Is there a more safe way to do this? The only thing I would do with the function is changing its

词法分析

江枫思渺然 提交于 2020-01-05 01:29:02
从左至右地对源程序进行扫描,按照语言的词法规则识别各类单词,并产生以{种别码,属性}为格式的结果。 <字母> => a|b|c...x|y|z <数字> => 0|1|2...7|8|9 <数字常数> => <数字>|<数字常数><数字>|<数字常数>.<数字常数> <标识符> => <字母>|<标识符><字母>|<标识符><数字> <关键字> => begin|if|then|while|do|end <运算符> => +|-|*...>|>=|= <界符> => ;|(|)|# #include<stdio.h> #include<string.h> int Distinguish(char *input,char *output,int *pos); #define Max 1000 void main () { char input[Max]="",output[Max*5]=""; int pos=0; printf("input:"); gets(input); while(Distinguish(input,output,&pos)); printf("%s",output); } int Distinguish(char *input,char *output,int *pos) { while(input[*pos]=='\n'||input[*pos]=='\t

c++ strcat

假装没事ソ 提交于 2020-01-04 09:38:08
来源: CSDN 作者: ma_zhao_shuai 链接: https://blog.csdn.net/weixin_41913666/article/details/103792468

strcat eccentric behavior

China☆狼群 提交于 2020-01-04 04:21:07
问题 I wrote this simple C program and couldn't quite figure out this bizarre behavior of strcat long sum(long col, char* path, char* path2){ printf("%s\n",path2); strcat(path,".endlines"); printf("%s\n",path2); return 0; } int main(int argc, char* argv[]) { int n=atoi(argv[1]); sum(n,argv[2],argv[3]); exit(EXIT_SUCCESS); } strcat is applied on path, but path2 is eventually modified as well. I would very much appreciate if someone let me know what was happening :) thanks Run ./program 3 example

实现strcat函数,assert宏(断言宏)使用介绍

可紊 提交于 2019-12-30 04:55:24
strcat函数的实现代码如下: char* MyStrcat(char *dst, const char *src) { assert(dst != NULL && src != NULL); char *temp = dst; while (*temp != '\0') temp++; while ((*temp++ = *src++) != '\0'); return dst; } 上述代码为strcat函数的源码,这里assert函数,其实是一个宏的功能可以通过man assert命令来了解,需要assert.h头文件,这里最好不要用strlen()函数去获得src字符串的长度,因为strlen包含在string.h头文件,这个头文件已经包含了strcat函数了,再说我们要实现功能一般是不用其他已有的函数的(特制一些函数是用头文件包含起来的,当然想stdio.h之类的除外), assert()宏用法: 断言捕捉不应该发生的非法情况 if(假设成立) { 程序正常运行; } else { 报错&&终止程序!(避免由程序运行引起更大的错误) } 功能:在运行过程中,如果assert 的参数为假,那么程序就会中止(一般地还会出现提示对话,说明在什么地方引发了assert)。 注意要点可以查看: https://www.cnblogs.com/thisway/p/5558914