Hi there,

The following program serves no purpose other than to help me understand the interaction between pointers, structs and arrays. There are two problems that I cannot figure out.

First, when executed, theStudentPtr->name does not display the string passed to the function.

Second, when you uncomment the printf at line 21, the number variable in the student struct goes haywire. This seems like a really bizarre thing to happen.

Code:
#include <stdio.h>

struct student
{
	char name[5] ;
	int number;
};

struct student * getStudent(char aName[], int nr);

int main()
{
	int numb = 10;
	char name[5] = "Davo";
	struct student *theStudentPtr;

	theStudentPtr = malloc(sizeof(struct student));

	theStudentPtr = getStudent(name, numb);

	//printf("name: %s \n\n", name); 

	printf("name: %s \nnumber: %i\n", theStudentPtr->name, theStudentPtr->number); 
	return 0;

}

struct student * getStudent(char *aName, int nr)
{
	struct student aStudent, *aStudentPtr;

	aStudent.number = nr;
	strcpy(aStudent.name, aName);

	aStudentPtr = &aStudent;

	return aStudentPtr;
}
So, I have only really confounded my understanding with this little program. Anyway, if anyone can help me through this, that would be tops.