FAQ Database Discussion Community


Char array in linked list causes core dump

c,arrays,linked-list,strcpy
I created a linked list program it works perfect with ints in c. but if change the parameter to char array, and try to do a strcpy it causes a core dump. #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char mac[25]; struct node * next; }; typedef struct...

Copying float values into char array

c,string,strcpy,strcat
I'm writing a TCP socket in C to send location data for a project I'm working on. So far, everything works, but I'm struggling with this seemingly simply problem. I'm trying to build a JSON String that will be sent over the socket. I have a character array (representative of...

What does `strcpy(x+1, SEQX)` do?

c,strcpy
I'm wondering what this syntax of strcpy() does in line 65 and 66: 24 #define SEQX "TTCATA" 25 #define SEQY "TGCTCGTA" 61 M = strlen(SEQX); 62 N = strlen(SEQY); 63 x = malloc(sizeof(char) * (M+2)); /* +2: leading blank, and trailing \0 */ 64 y = malloc(sizeof(char) * (N+2)); 65...

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

c,arrays,strcpy
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...

Strcpy just crashes the second time I use in the same context

c,string,segmentation-fault,dynamic-memory-allocation,strcpy
This code waits for a string entered by the user, then the program should sort it immediately in an pointer array. The problem is in the case cop<0. I don't know what's the problem with strcpy() . If anyone could help, I'll be thankful. Here is the code : #include...

strcpy adding random numbers to empty string

c,string,strcpy,c-strings
I am trying to flush out a char buffer by using strcpy(buffer, ""). strcpy() seems to be putting random numbers in the string. In gdb, I see the buffers (received_message): Before strcpy() call: (gdb) print received_message $6 = "9210070627\000\000\000\000\000\000\000\000\000" After strcpy() call: (gdb) print received_message $8 = "\000\062\061\060\060\067\060\066\062\067\000\000\000\000\000\000\000\000\000" Where \060...

How to add null terminator to char pointer, when using strcpy

c,pointers,char,malloc,strcpy
I have a program that's attempting to use the strcpy() function. I know that when one uses a char array such as: char array[10] the null terminator can be set by: array[0] = '\0'; However, how would I go about setting the null terminator(s) when using char pointers? EDIT: The...

C - strcpy with malloc size less than argument's size [duplicate]

c,malloc,strcpy
This question already has an answer here: I can use more memory than how much I've allocated with malloc(), why? 15 answers char* init_array() { const int size = 5; char *p = (char*) malloc(size * sizeof(char)); strcpy(p, "Hello, world! How are you?"); return p; } with size =...

is this code correct?If yes then malloc is already assigning the addresses to name[i] variable then why strcpy is used?

c,string,pointers,malloc,strcpy
Following is the piece of code char str[20]; char *name[5]; for(i=0;i<5;i++){ printf("Enter a string"); gets(str); name[i]=(char *)malloc(strlen(str)); strcpy(name[i],str); } When in line 5 address of each string(denoted by str variable) is stored in name[i] array, then why this code is copying each address into name[i] using strcpy()?...

Garbage with strcpy and strcat

c,malloc,strcpy,strcat
I'm doing a client-server project in linux and I need to concatenate some strings. I tried my code on visual studio in windows and it works fine, but it linux it gives me some garbage. I've got this function: char* concat(char s1[], char s2[]) { int tam = 0; tam...