strcpy

Getting segmentation fault strcpy [duplicate]

痞子三分冷 提交于 2019-12-25 05:11:14
问题 This question already has answers here : Segmentation Fault in strcpy() (3 answers) Closed 4 years ago . struct Object * newObj(char * nome, int idade, float altura) { struct Object *obj = (struct Object *) malloc(sizeof(struct Object)); strcpy(obj->nome, nome); // This is the line obj->idade = idade; obj->altura = altura; return obj; } This is my code, I don't know why I'm getting segmentation fault in strcpy. Any ideas? Thanks in advance. 回答1: In your struct Object type, the nome member is

What's wrong with my strcpy? [closed]

女生的网名这么多〃 提交于 2019-12-24 22:36:07
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I tried to make strcpy myself. It should work, I even copied and pasted the (almost exact code) from someones post here about strcpy . Both give me a

How could I copy data that contain '\0' character

烂漫一生 提交于 2019-12-24 17:31:23
问题 I'm trying to copy data that conatin '\0'. I'm using C++ . When the result of the research was negative, I decide to write my own fonction to copy data from one char* to another char*. But it doesn't return the wanted result ! My attempt is the following : #include <iostream> char* my_strcpy( char* arr_out, char* arr_in, int bloc ) { char* pc= arr_out; for(size_t i=0;i<bloc;++i) { *arr_out++ = *arr_in++ ; } *arr_out = '\0'; return pc; } int main() { char * out= new char[20]; my_strcpy(out,

invalid conversion from 'char' to 'char*' using strcpy

旧时模样 提交于 2019-12-24 02:15:42
问题 Ok so here are the parts of my code that I'm having trouble with: char * historyArray; historyArray = new char [20]; //get input cin.getline(readBuffer, 512); cout << readBuffer <<endl; //save to history for(int i = 20; i > 0; i--){ strcpy(historyArray[i], historyArray[i-1]); //ERROR HERE// } strcpy(historyArray[0], readBuffer); //and here but it's the same error// The error that i'm receiving is: "invalid conversion from 'char' to 'char*' initializing argument 1 of 'char* strcpy(char*, const

嵌入式软件工程师经典面试题

[亡魂溺海] 提交于 2019-12-23 21:01:56
1、int a[10]={1,2,3,4,5,6,7,8,9,0}; int *p=&a[1]; 则p[6]等于8 2、整数数组清零:bzero(),memset()。 3、sizeof();测试变量所占地址的字节数 4、 main() { char *str[]={“ab”,“cd”,“ef”,“gh”,“ij”,“kl”}; char t; t=(str+4)[-1]; printf("%s",t); }则显示"gh" 5、小端:低位字节数据存储在低地址 大端:高位字节数据存储在低地址 例如:int a=0x12345678;(a首地址为0x2000) 0x2000 0x2001 0x2002 0x2003 0x12 0x34 0x56 0x78 大端格式 6、异步IO和同步IO区别 如果是同步IO,当一个IO操作执行时,应用程序必须等待,直到此IO执行完,相反,异步IO操作在后台运行, IO操作和应用程序可以同时运行,提高系统性能,提高IO流量; 在同步文件IO中,线程启动一个IO操作然后就立即进入等待状态,直到IO操作完成后才醒来继续执行,而异步文件IO中, 线程发送一个IO请求到内核,然后继续处理其他事情,内核完成IO请求后,将会通知线程IO操作完成了。 7、用变量a定义 一个整型数 int a; 一个指向整型数的指针 int a; 一个指向指针的指针

栈的链式存储框架

只愿长相守 提交于 2019-12-23 19:03:57
定义   栈是限定只能在表尾删除和插入操作的线性表。   允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom)。栈又称为后进先出(Last In First Out)的线性表,简称LIFO结构。   栈的插入操作称为进栈,也称压栈、入栈。   栈的删除操作称为出栈,也称弹栈。 栈的抽象数据结构   由于栈本身就是一个线性表,所以线性表的操作特性它都具备,针对它的特殊性,在它的操作上可能会有一些变化。将进栈和出栈分别改名为push和pop。   由于栈本身是一个线性表,所以线性表的顺序存储结构和 链式存储结构 同样适用于栈。 栈的链式存储框架搭建 链式栈的结点 typedef struct LINKNODE { struct LINKNODE* next; }LinkNode; 链式栈 typedef struct LINKSTACK { LinkNode head; int size; }LinkStack; 构架搭建 //初始化栈 LinkStack *Init_LinkStack(); //入栈 void Push_LinkStack(LinkStack* stack, LinkNode* data); //返回栈顶元素 LinkNode* Top_LinkStack(LinkStack* stack); //出栈 void Pop_LinkStack

What is the difference between pointer and array in the following context?

风流意气都作罢 提交于 2019-12-23 15:27:46
问题 #include <cstring> int main() { char *pName = new char[10]; char dummy[] = "dummy"; strcpy(pName + 0,dummy);//how this is different from -->this works strcpy(pName[0],dummy);//this one...--> error C2664: 'strcpy' : //cannot convert parameter 1 //from 'char' to 'char *' } 回答1: pName[0] is the first element in a character array ( one character) pName is a shortcut to &pName[0] (a pointer to the first element of your array) The reason you are getting your error is because strcpy expects a

C error:format '%s' expects argument of type 'char *'but argument 2 has type 'char (*)[100]'

ⅰ亾dé卋堺 提交于 2019-12-23 12:53:53
问题 I'm working on an exercise in c the last few days and I'm having this warning (as the title suggests). I've tried a bunch of stuff but I don't really know how to exactly fix this. I'm not good at programming so there are mistakes. Below are the structs I'm using (which cannot be changed because that's how they are given): typedef struct bookR* book; struct bookR{ char author[MAXSTRING]; enum genres{fiction,scientific,politics}; int id; char review[MAXLINES][MAXSTRING]; }; typedef struct nodeR

【C】常用的字符串函数

此生再无相见时 提交于 2019-12-23 12:12:57
1. strcpy   函数名:strcpy   用法:char *strcpy(char *destin, char *cource)   功能:将一个字符串从一个拷贝到另外一个   程序示例:    1 #include <stdio.h> 2 #include <string.h> 3 4 int main(){ 5 char str1[] = "source"; 6 char str2[] = "des"; 7 8 strcpy(str1,str2); 9 printf("str1 : %s\n",str1); 10 return 0; 11 }   程序输出:    2. strnpy   函数名:strnpy   用法:char * strncpy(char *dest, char *src,size_t n);   功能:将字符串src中的前n个字符复制到字符串数组dest中,注意(不会清除dest数组中原来的数据,只是将新的数据覆盖)   程序示例:    1 #include <stdio.h> 2 #include <string.h> 3 4 int main(){ 5 char str1[] = "source"; 6 char str2[] = "dest"; 7 8 strncpy(str1,str2,4); 9 printf("str1 : %s\n"

Is strcpy_s part of the C++ Standard? Or only part of MS Visual C++

蓝咒 提交于 2019-12-20 07:29:15
问题 Using the function strcpy in MS Visual Studio gives me an error saying I should use strcpy_s which is safer to use. Is strcpy_s part of the C++ standard? Or is it only part of Microsoft Visual C++? Will code containing strcpy_s only compile in Visual Studio? 回答1: strcpy_s() is an optional part of C11 (more formally called a "conditional feature". Implementations are permitted to not implement the "Bounds-checking interfaces" standardized in Annex K. Some other conditional features of C11