M
Martin Joergensen
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,
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;
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,
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,
(*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 ??? */
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...
Best regards / Med venlig hilsen
Martin Jørgensen
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,
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;
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,
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,
(*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 ??? */
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...
Best regards / Med venlig hilsen
Martin Jørgensen