a question on pointers, structs and arrays

This is a discussion on a question on pointers, structs and arrays within the C Programming forums, part of the General Programming Boards category; Hi there, The following program serves no purpose other than to help me understand the interaction between pointers, structs and ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Brisbane, Australia
    Posts
    7

    a question on pointers, structs and arrays

    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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well, you have a few important things to note:

    • You malloc() a struct student inside main() and then reassign the pointer without saving the value you received or free()ing the memory. This is a memory leak. Generally speaking, you should free() everything you malloc().
    • You have a function to allocate and return a struct student. This is good form, however, you are returning an address of a local variable. This is wrong completely. Local variables are destroyed (not really, but the explanation is beyond the scope of this post) upon function return. Therefore whatever data you are pointing to in main() that is returned from this function will most likely eventually be overwritten.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Brisbane, Australia
    Posts
    7
    Of course!! Thanks. I fell foul of your second point because I wanted to observe a function that returns a pointer. So I am now passing in the pointer.

    Thanks very much. So helpful.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    A note here. Just remember that this is faulyy code and you shouldn't use it, but I'll use it for demonstrating purposes.

    There's no need to create another variable of type pointer and assign the address to that. Since the return type is a pointer, all you need to do is:

    Code:
    struct student * getStudent(char *aName, int nr)
    {
    	struct student aStudent;
    	aStudent.number = nr;
    	strcpy(aStudent.name, aName);
    	return &aStudent;
    }
    I could return the variable directly with a & before, so as to take its address. IT works fine.
    But this is very bad code, so don't go around returning a local variable through a pointer!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays, Structs, Pointers, and unexpected crashes
    By KairosDrasis in forum C Programming
    Replies: 2
    Last Post: 04-13-2009, 12:37 AM
  2. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 10:25 AM
  3. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM
  4. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM
  5. Pointers and Arrays
    By C Seņor in forum C Programming
    Replies: 4
    Last Post: 06-16-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21