strcpy

How to assign string of one variable to other variable?

瘦欲@ 提交于 2020-01-11 14:05:16
问题 This is my first question on this site. How do i assign string of one variable to other variable. What am i doing wrong here? #include<stdio.h> #include<string.h> main(){ char a[30],b[30]; scanf("%s",a); b[30]=a[30]; printf("%s",b); } 回答1: Use the standard C function strcpy declared in the header <string.h> . For example strcpy( b, a ); Arrays do not have the assignment operator. As for your statement b[30]=a[30]; then b[30] and a[30] are undefined objects of the type char that are beyond the

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好在:

c语言中的string

百般思念 提交于 2020-01-10 12:28:24
1. strlen(char const* s);   函数传入的是c风格字符串(即以‘\0’结尾的字符数组),返回的长度为size_t(即unsigned int),其长度不包括'\0'。 2. strcpy(char* dest, char const* source);   dest:目标指针;   source:是源指针,传入的必须是c风格字符串或者字符数组。   返回值: dest指针   注意:1. 该函数会将包括'\0'在内的source全部拷贝到dest。如果char* source="abcd"; dest为char[],则dest应该定义为char[5];       2. src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。       3. C语言中不支持用赋值符号“=”直接将一个字符数组赋值给另一个字符数组。因为C语言不支持运算符重载。故而必须用strcpy操作。 3. strncpy(char* dest, char const* source, size_t n);   n代表可以指定字符个数进行赋值。   功能:将字符串source中最多n个字符复制到字符数组dest中(它并不像strcpy一样遇到NULL才停止复制,而是等凑够n个字符才开始复制),返回指向dest的指针。要求:如果n > dest串长度

C++之string类型入门(二):类型转化

怎甘沉沦 提交于 2020-01-10 00:47:19
文章目录 一、其他类型转string 1.数字类型转string:to_string() 二、string转其他类型 1.string转c_str (1)直接使用 (2)开辟char*内存空间,strcpy()赋值 (3)const char*直接赋值 2.string转数字类型 一、其他类型转string 1.数字类型转string:to_string() string std :: to_string ( int ) string std :: to_string ( long ) string std :: to_string ( unsigned long ) string std :: to_string ( long long ) string std :: to_string ( unsigned long long ) string std :: to_string ( float ) string std :: to_string ( double ) string std :: to_string ( long double ) 例如: # include <iostream> # include <cstring> using namespace std ; int main ( ) { int num_int = 123 ; long num_long =

C++ classes getting value using pointers and strcpy [closed]

一曲冷凌霜 提交于 2020-01-08 01:25:34
问题 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 years ago . I am trying to understand C++. Can anybody explain what this code does exactly, I understood that it is some type of setter and getter in Java but I am not sure. Comm::Comm(const char* id) { strcpy(this->id, id); } char* Comm::getId() { return id; } 回答1: What does this code do? It burns the eyes of children. The

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

Why can't I dynamically allocate memory of this string of a struct?

佐手、 提交于 2020-01-06 19:49:13
问题 Let's say for example, I have a struct: typedef struct person { int id; char *name; } Person; Why can't I do the following: void function(const char *new_name) { Person *human; human->name = malloc(strlen(new_name) + 1); } 回答1: You need to allocate space for human first: Person *human = malloc(sizeof *human); human->name = malloc(strlen(new_name) + 1); strcpy(human->name, new_name); 回答2: You have to allocate memory for the structure Person . The pointer should point to the memory allocated

Dynamically construct a char in C

狂风中的少年 提交于 2020-01-01 19:31:30
问题 I am trying to construct an array that has a series of character that I want to construct in the fly, the characters are like this \x01 , \x02 and so on. For example, lets say we have: #define NUMCOLORS 3 char delim[NUMCOLORS]; And we want to have in delim the values \x01, \x02, \x03 . I thought two possible ways to do it, but both cause a segfault: for (int i = 0; i < NUMCOLORS; i++){ char *h = "\x01"; sprintf(h, "\x0%d", i+1); strcpy(delim[i], h); } // Other way for (int i = 0; i <

实验七

六眼飞鱼酱① 提交于 2020-01-01 07:15:14
#include <stdio.h> #include <stdlib.h> #include <string.h> const int N = 10; // 定义结构体类型struct student,并定义其别名为STU typedef struct student { long int id; char name[20]; float objective; /*客观题得分*/ float subjective; /*操作题得分*/ float sum; char level[10]; }STU; // 函数声明 void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { STU stu[N]; printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n"); process(stu, N); printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n"); output(stu, N); system("pause"); return 0; } //

Using passed-in array to initialize other array

社会主义新天地 提交于 2019-12-25 05:14:28
问题 I am trying to "initialize" an array that I have made in my class declaration and I do not know what I am doing wrong. I understand that when you pass an array into a function, it decays into a pointer to the first character. The code breaks on my second strcpy() line but I am not sure what it is I am doing wrong (I have very little experience with strcpy()). My code reads as follows: class TestClass { public: TestClass(char []); ~TestClass(); void Append(TestClass); char* m_string; };