This code is a part of a bigger program. I want to create an array of structs. The struct contains two data types: char
and char*
, each of which gets an input from the user. I'm a rookie and not able to find my way through this. Please help.
Error generated: Segmentation Fault 11.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char ch;
char str[20];
}typ;
typ* add(char* str, char ch);
int main(int argc, char const *argv[]){
typ* arr;
arr = (typ*)malloc(sizeof(typ));
char* name;
name = (char*)malloc(10);
*(name + 0)= 'c';
*(name + 1)= 'a';
*(name + 2)= 'p';
*(name + 3)= '\0';
char c = 'k';
arr = add(name, c);
printf("%c\n", arr->ch );
printf("%s\n", arr->str );
return 0;
}
typ* add(char* str, char ch){
typ tempo;
strcpy(str, tempo.str);
printf("%s\n", str);
tempo.ch = ch;
typ* temp;
*temp = tempo;
return temp;
}