Jump to content
  • Advertisement
Sign in to follow this  
darthstrife

Help with Dynamic Arrays

This topic is 4159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to read in information from a file, and after so many known entries, the file then declares how many more entries I will have before giving them to me. Therefore, I want to read that number and then dynamically adjust my array size accordingly. What am I doing wrong? Here's my code:
#include <stdio.h>
#define SIZE 8

struct info{
	char location[20];
	char company[20];
	int sales_month1;
	int sales_month2;
	int sales_month3;
	int sales_month4;
	char manager[30];};

void print_file(struct info SALES[]);

int main(void){
	struct info *SALES;
	SALES = malloc(SIZE * sizeof(int));

	print_file(SALES);
	
	system("PAUSE");
	return 0;
}

void print_file(struct info SALES[]){
	int i, numinput, sum;
	FILE *infile;
	char filename[20];

	printf("Enter the filename: ");
	scanf("%s", filename);
	infile = fopen(filename, "r");

	for(i=0; i<SIZE; i++){
		fscanf(infile, "%s", SALES.location);
		fscanf(infile, "%s", SALES.company);
		fscanf(infile, "%d", &SALES.sales_month1);
		fscanf(infile, "%d", &SALES.sales_month2);
		fscanf(infile, "%d", &SALES.sales_month3);
		fscanf(infile, "%d", &SALES.sales_month4);
		fscanf(infile, "%s", SALES.manager);}

	fscanf(infile, "%d", &numinput);
	SALES = realloc(SALES, sizeof(int) * (SIZE + numinput));
	sum = SIZE + numinput;

	for(i=SIZE; i<sum; i++){
		fscanf(infile, "%s", SALES.location);
		fscanf(infile, "%s", SALES.company);
		fscanf(infile, "%d", &SALES.sales_month1);
		fscanf(infile, "%d", &SALES.sales_month2);
		fscanf(infile, "%d", &SALES.sales_month3);
		fscanf(infile, "%d", &SALES.sales_month4);
		fscanf(infile, "%s", SALES.manager);}

	for(i=0; i<sum; i++){
		printf("%s ", SALES.location);
		printf("%s ", SALES.company);
		printf("%d ", SALES.sales_month1);
		printf("%d ", SALES.sales_month2);
		printf("%d ", SALES.sales_month3);
		printf("%d ", SALES.sales_month4);
		printf("%s\n", SALES.manager);
	}

	fclose(infile);
}

Share this post


Link to post
Share on other sites
Advertisement
Well, it is incorrect that you are allocating sizeof(int) * SIZE bytes, and treating that buffer as an array of SIZE info structs -- since sizeof(info) is obviously larger than sizeof(int), you don't have enough memory allocated there and you're overstepping the array bounds. In both the malloc() and realloc() calls, replace sizeof(int) with sizeof(info).

I haven't used C and the C IO routines for some time, so I may easily be overlooking something else.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Well, it is incorrect that you are allocating sizeof(int) * SIZE bytes, and treating that buffer as an array of SIZE info structs -- since sizeof(info) is obviously larger than sizeof(int), you don't have enough memory allocated there and you're overstepping the array bounds. In both the malloc() and realloc() calls, replace sizeof(int) with sizeof(info).

I haven't used C and the C IO routines for some time, so I may easily be overlooking something else.


in replacing sizeof(int) with sizeof(info) i get the following error:

1>.\dynamic1.c(17) : error C2065: 'info' : undeclared identifier
1>.\dynamic1.c(17) : warning C4047: '=' : 'info *' differs in levels of indirection from 'int'
1>.\dynamic1.c(44) : warning C4047: '=' : 'info *' differs in levels of indirection from 'int'

Share this post


Link to post
Share on other sites
Quote:
Original post by DeafManNoEars
Try this my friend:

SALES = (info*)malloc(SIZE * sizeof(info));


I get this error:

1>.\dynamic1.c(17) : error C2065: 'info' : undeclared identifier
1>.\dynamic1.c(17) : error C2059: syntax error : ')'

Share this post


Link to post
Share on other sites
Change the structure to:


typedef struct
{
char location[20];
char company[20];
int sales_month1;
int sales_month2;
int sales_month3;
int sales_month4;
char manager[30];
} info;





Then you can declare a pointer by writing


info* sales_ptr;





and use sizeof( info ) to get its size


sales_ptr = (info*) malloc( sizeof( info ) * array_size );





[Edited by - kiome on February 2, 2007 10:28:39 AM]

Share this post


Link to post
Share on other sites
I tried the typedef struct. Here is my new code, but I'm still getting errors which I included below the code:

#include <stdio.h>
#define SIZE 8

typedef struct{
char location[20];
char company[20];
int sales_month1;
int sales_month2;
int sales_month3;
int sales_month4;
char manager[30];
}info;

void print_file(struct info SALES[]);

int main(void){
info* SALES;
SALES = malloc(sizeof(info) * SIZE);

print_file(SALES);

system("PAUSE");
return 0;
}

void print_file(struct info SALES[]){
int i, numinput, sum;
FILE *infile;
char filename[20];

printf("Enter the filename: ");
scanf("%s", filename);
infile = fopen(filename, "r");

for(i=0; i<SIZE; i++){
fscanf(infile, "%s", SALES.location);
fscanf(infile, "%s", SALES.company);
fscanf(infile, "%d", &SALES.sales_month1);
fscanf(infile, "%d", &SALES.sales_month2);
fscanf(infile, "%d", &SALES.sales_month3);
fscanf(infile, "%d", &SALES.sales_month4);
fscanf(infile, "%s", SALES.manager);}

fscanf(infile, "%d", &numinput);
SALES = realloc(SALES, sizeof(info) * (SIZE + numinput));
sum = SIZE + numinput;

for(i=SIZE; i<sum; i++){
fscanf(infile, "%s", SALES.location);
fscanf(infile, "%s", SALES.company);
fscanf(infile, "%d", &SALES.sales_month1);
fscanf(infile, "%d", &SALES.sales_month2);
fscanf(infile, "%d", &SALES.sales_month3);
fscanf(infile, "%d", &SALES.sales_month4);
fscanf(infile, "%s", SALES.manager);}

for(i=0; i<sum; i++){
printf("%s ", SALES.location);
printf("%s ", SALES.company);
printf("%d ", SALES.sales_month1);
printf("%d ", SALES.sales_month2);
printf("%d ", SALES.sales_month3);
printf("%d ", SALES.sales_month4);
printf("%s\n", SALES.manager);
}

fclose(infile);
}


1>------ Build started: Project: assign1, Configuration: Debug Win32 ------
1>Compiling...
1>dynamic1.c
1>.\dynamic1.c(18) : warning C4047: '=' : 'info *' differs in levels of indirection from 'int'
1>.\dynamic1.c(20) : warning C4133: 'function' : incompatible types - from 'info *' to 'info *'
1>.\dynamic1.c(32) : warning C4996: 'scanf' was declared deprecated
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\stdio.h(295) : see declaration of 'scanf'
1> Message: 'This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
1>.\dynamic1.c(33) : warning C4996: 'fopen' was declared deprecated
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\stdio.h(234) : see declaration of 'fopen'
1> Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
1>.\dynamic1.c(36) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(36) : error C2037: left of 'location' specifies undefined struct/union 'info'
1>.\dynamic1.c(37) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(37) : error C2037: left of 'company' specifies undefined struct/union 'info'
1>.\dynamic1.c(38) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(38) : error C2037: left of 'sales_month1' specifies undefined struct/union 'info'
1>.\dynamic1.c(39) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(39) : error C2037: left of 'sales_month2' specifies undefined struct/union 'info'
1>.\dynamic1.c(40) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(40) : error C2037: left of 'sales_month3' specifies undefined struct/union 'info'
1>.\dynamic1.c(41) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(41) : error C2037: left of 'sales_month4' specifies undefined struct/union 'info'
1>.\dynamic1.c(42) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(42) : error C2037: left of 'manager' specifies undefined struct/union 'info'
1>.\dynamic1.c(45) : warning C4047: '=' : 'info *' differs in levels of indirection from 'int'
1>.\dynamic1.c(49) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(49) : error C2037: left of 'location' specifies undefined struct/union 'info'
1>.\dynamic1.c(50) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(50) : error C2037: left of 'company' specifies undefined struct/union 'info'
1>.\dynamic1.c(51) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(51) : error C2037: left of 'sales_month1' specifies undefined struct/union 'info'
1>.\dynamic1.c(52) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(52) : error C2037: left of 'sales_month2' specifies undefined struct/union 'info'
1>.\dynamic1.c(53) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(53) : error C2037: left of 'sales_month3' specifies undefined struct/union 'info'
1>.\dynamic1.c(54) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(54) : error C2037: left of 'sales_month4' specifies undefined struct/union 'info'
1>.\dynamic1.c(55) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(55) : error C2037: left of 'manager' specifies undefined struct/union 'info'
1>.\dynamic1.c(58) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(58) : error C2037: left of 'location' specifies undefined struct/union 'info'
1>.\dynamic1.c(59) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(59) : error C2037: left of 'company' specifies undefined struct/union 'info'
1>.\dynamic1.c(60) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(60) : error C2037: left of 'sales_month1' specifies undefined struct/union 'info'
1>.\dynamic1.c(61) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(61) : error C2037: left of 'sales_month2' specifies undefined struct/union 'info'
1>.\dynamic1.c(62) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(62) : error C2037: left of 'sales_month3' specifies undefined struct/union 'info'
1>.\dynamic1.c(63) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(63) : error C2037: left of 'sales_month4' specifies undefined struct/union 'info'
1>.\dynamic1.c(64) : error C2036: 'info *' : unknown size
1>.\dynamic1.c(64) : error C2037: left of 'manager' specifies undefined struct/union 'info'
1>Build log was saved at "file://c:\Documents and Settings\Andrew\My Documents\Visual Studio 2005\Projects\computer science 1\assign1\assign1\Debug\BuildLog.htm"
1>assign1 - 42 error(s), 5 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
Ok, some fixes and some suggestions (not strictly required, but possibly better).

1. Suggestion, perhaps rename SIZE to something like NUM_ITEMS or something, so you have no possibility of confusing it with values like sizeof(info) [struct size] or sizeof(info) * NUM_ITEMS [array size].

2. line 18. Use the cast shown by DeafManNoEars.

3. line 20. Not sure here. Perhaps line 28

void print_file(struct info SALES[]){

should be

void print_file(info SALES[]){

or

void print_file(info *SALES){

but I didn't think it mattered in C.

4. line 32. Ignore this BS, this is MS saying that the C standard scanf function is too dangerous for programmer's to use because it can blow up in certain cases (depending on the requested type and the input given) and they want you to use an alternative ... I don't remember what, something like nscanf or some such ...

5. Suggestion. You have a strange naming convention where your type "info" is all lower and your variable "SALES" is all caps. If you want to keep it, feel free. But you use lower variable names for stuff like i, numinput, sum, filename - so I don't think you means to do this. I think you mean to name if "INFO" and "sales" to match you other conventions and be much more obvious to readers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!