When I use the whole code in the main function it works perfectly but now I want to use the function for some "Strings" which I initialize in a 2D-Array.
The idea behind the function is to create a product of a struct globally initialized. The line with strcpy
gives the error:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
I am using Xcode 11.3.1 on a Mac.
Can you guys help me with that?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
struct produkt neues_angebot(char *produktname);
struct produkt{
char name[5];
int produkt_zahl;
float produkt_preis;
};
struct produkt neues_angebot(char *produktname){
time_t t;
srand((unsigned) time(&t));
struct produkt Zwischenname = {
"xxx",(rand() % 49),((rand()% 600)/100)
};
strcpy(Zwischenname.name, produktname);
return Zwischenname;
}
int main(int argc, const char * argv[]) {
char teste[]="hello";
printf("%s\n",neues_angebot(teste).name);
}