Re: first program with pointers to structures and realloc....
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: 30 Apr 2006 11:00:56 +0200
Martin Joergensen wrote:
Hi,
I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to keep track on the structures... But it's a bit confusing - my program won't compile and I don't know what to do about the warnings/error messages.
c:\documents and settings\dell\Desktop\test\main.c(5) : warning C4115: 'tablevalues' : named type definition in parentheses
c:\documents and settings\dell\Desktop\test\main.c(41) : warning C4133: 'function' : incompatible types - from 'tablevalues *' to 'tablevalues *'
c:\documents and settings\dell\Desktop\test\main.c(75) : error C2065: 'tablevalues' : undeclared identifier
c:\documents and settings\dell\Desktop\test\main.c(80) : warning C4133: '=' : incompatible types - from 'int *' to 'tablevalues *'
c:\documents and settings\dell\Desktop\test\main.c(84) : error C2107: illegal index, indirection not allowed
- - - - - - - - - - - - - - - - - - - -
#include <stdlib.h>
#include <stdio.h>
void save_in_memory( struct tablevalues *data_pointer,
You haven't yet told the compiler what type struct tablevalues is! In C you should always declare things before using them.
unsigned *number_of_sets_in_memory,
unsigned increase_memory_number,
double double_x, int var1,
int var2, int var3);
/* ************ Main program ************ */
int main()
{
const unsigned increase_memory_number = 3;
unsigned number_of_sets_in_memory;
double doub_x;
int va1, va2, va3;
typedef struct
{
double double_x;
int var1;
int var2;
int var3;
} tablevalues;
Here you declare a local definition of a structure, but it has absolutely nothing to do with anything outside of main. Also, this does not define struct tablevalues, it defines an unnamed struct and gives it an alias that is simply tablevalues.
tablevalues *data_pointer;
number_of_sets_in_memory = 0; /* there's nothing stored at this moment */
/* get user input - and store the data */
printf("Enter a double value, followed by 3 integers:\n");
while( scanf("%lf %d %d %d\n", &doub_x, &va1, &va2, &va3) == 4)
{
save_in_memory( data_pointer,
&number_of_sets_in_memory,
increase_memory_number,
doub_x, va1,
va2, va3);
printf("Enter single double value, followed by 3 integers:\n");
}
exit(EXIT_SUCCESS);
}
/* ************ Function ************ */
void save_in_memory( struct tablevalues *data_pointer,
By here, your typedef has gone out of scope.
unsigned *number_of_sets_in_memory,
unsigned increase_memory_number,
double double_x, int var1,
int var2, int var3)
{
unsigned i; /* counter */
int *tmp_ptr;
/* update counter of how much is saved currently */
*number_of_sets_in_memory++;
/* need to allocate more memory ? */
if(*number_of_sets_in_memory % increase_memory_number == 0)
{
/* first allocate space for the pointer array */
if ((tmp_ptr = realloc(data_pointer,
The only data_pointer is a variable local to main, how do you expect this function to see it?
(*number_of_sets_in_memory + increase_memory_number)
* sizeof(tablevalues) )) == NULL) {
printf("Memory failure!\n\n");
exit(EXIT_FAILURE);
}
/* ??? possibly I need to copy the old values to the new array ??? */
Read your C text book. It will tell you that realloc is defined as handling that for you.
data_pointer = tmp_ptr;
}
/* Ok, enough memory available - now: store information in structure-1 */
data_pointer[number_of_sets_in_memory-1]->double_x = double_x;
data_pointer[number_of_sets_in_memory-1]->var1 = var1;
data_pointer[number_of_sets_in_memory-1]->var2 = var2;
data_pointer[number_of_sets_in_memory-1]->var3 = var3;
}
- - - - - - - - - - - - - - - - - - - -
If somebody can help me get the program to work, I would be glad. Then I want to debug it and watch what happens different places in memory... :-)
One other thing on my mind is: Perhaps the "data_pointer" variable should have been an ** pointer instead of a * pointer. I actually got the whole program to compile and run using a ** pointer (without warnings/errors), but then I convinced myself that a *-pointer would be just as good and I modified the whole program... I would appreciate any comments on this...
Now I can't remember what I did when I got the program to work using a **-data_pointer... :-)
Well, I'm sure there must have been more differences than you claim.
You need to read the early chapters of your C text book again. The ones where it talks about scope, local variables, and defining things before using them.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
.
- References:
- first program with pointers to structures and realloc....
- From: Martin Joergensen
- first program with pointers to structures and realloc....
- Prev by Date: Re: help me learn C
- Next by Date: discussing C evolution
- Previous by thread: Re: first program with pointers to structures and realloc....
- Next by thread: on usenet by a "resident troll"
- Index(es):
Relevant Pages
|