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.
#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]).
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++.
#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);
}
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.
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.
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...