Pointers with Memory Allocation

I have been struggling with this assignment for a while now, with no success to speak of.

I don't have my code right now (I will post it when I get home), but I will post the assignment details in case anyone could point my in the right direction. I really appreciate all help.


Pointers with memory allocation

Given the following program, which uses a pointer *info to the structure NameAgeInfo containing the fixed-size name and age variables, modify the program to dynamically allocate memory for the name variable (since a name can vary quite a bit in size). You should convert the structure to contain a pointer variable *name, instead of the character array name[20], which will point to the beginning of the memory allocated by the compiler for holding the name information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>

struct NameAgeInfo
{
	char name[20];
	int age;
};

void GetNameAge(struct NameAgeInfo *info);

int main()
{
	struct NameAgeInfo data;

	GetNameAge(&data);
	printf ("Your name is %s and your age is %d.\n", data.name, data.age);
	return 0;
}

void GetNameAge(struct NameAgeInfo *info)
{
	printf ("What is your name? ");
	gets(info->name);
	printf ("How old are you? ");
	scanf(" %d", &info->age);
}


Ever heard of dynamic memory allocation? He just wants you to create an equivalent program that allocates the memory dynamically instead of statically (i.e. uses new to declare an array of size 20 instead of [20]).

Are you using C or C++ code?

If you're using C++ then here's the tutorial from this site:
http://www.cplusplus.com/doc/tutorial/dynamic/

If you're using C, read the tutorial, and look-up
"malloc" and "free" in the search bar up top. That will lead you to the ANSI C library pages for those functions. These are the C equivalents of new and delete from C++.

Thanks for the link jpeg..


Okay this is the best I could come up with and when I try to run it I get compile errors..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct NameAgeInfo
{
	char *name;
	int age;
};

int GetNameAge(char *info);

int main()
{
	char nullo='0';
	int age;
	struct NameAgeInfo data;

	age=GetNameAge(data.name);
	printf ("Your name is %s and your age is %d.\n", *data.name, age);
	return 0;
}

int GetNameAge(char *info)
{
	int storsize, age;
	char buffer;

	printf ("What is your name? ");
	for (storsize=1; getch()!='\r'; storsize++) {
	buffer += getch();
	}
	printf("\n");
	info=(char*) malloc (storsize+1);
	*info=buffer;

	printf ("How old are you? ");
	scanf(" %d", age);
	return(age);
	}


and the errors are...?
Note that I see the missing address symbol on age, but that isnt the issue.

Sorry.. I shouldn't have said compile error. After I compile and run I can go through the function and when I get to line 20 (the printf) (I added a getch) I get a "Debugger Exception Notifier" which tells me I have made an "Access violation"... This seems like some kind of memory storage issue but I cannot figure it out.

thanks..
Last edited on
Line 31 and the second parameter for printf() on line 20 indicate complete confusion about how C strings work.

We could explain it in great detail, but we would just be rewriting content from a textbook. Review your material on the subject, if you have any. If you don't, K&R's The C programming language is a good book. The second edition covers ANSI C, so this is the one you should look for.
Otherwise, take a look at the tutorial on this site.
Okay, I changed line 31 to:
1
2
	for (storsize=1; getch()!='\r'; storsize++) {
	buffer[storsize] = getch();   //line 31 


I changed the assignment operation of line 35 to:

strcpy(*info, buffer);

Still no success... For some reason once I get to line 35, the compiler gets confused. I do not understand.. I am copying a string to the pointer of a string...

Thanks for any help..
Is buffer a single char or an array?
Topic archived. No new replies allowed.