0

what's wrong here?

typedef struct person
{
	int age;
	char name[50];
};

int main()
{
	struct person test;

	test.age = 20;
	test.name = "Test";
	
	return 0;
}
2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by fatihpiristine
0

anyways i fixed it myself :) here is the solution

typedef struct
{
	int age;
	char *name[50];
} Person;

Person newperson(char *name, int age)
{
	Person temp;
	*temp.name = name;
	temp.age = age;
	return temp;
}

int main()
{
	Person list[50], ttt;

	list[0] = newperson("Test", 20);

	

	*ttt.name = "ASDF";

	
	
	return 0;
}
0

Use strcpy to copy a string. You've got more going wrong in that code, though. I think you're after something that might look a bit like this:

#include <string.h>

typedef struct
{
	int age;
	char name[50];
} Person;

Person newperson(char *name, int age)
{
	Person temp;
	strcpy(temp.name, name);
	temp.age = age;
	return temp;
}
0

thanks for the remark. that was the thing i was trying to remember.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.