6 Replies - 1490 Views - Last Post: 07 December 2010 - 12:55 PM Rate Topic: -----

#1 Bindayen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 10

Initializing a pointer to a structure

Posted 05 December 2010 - 05:12 PM

I've got a bit of a problem with code for an assignment, and I was hoping someone could help. It's pretty much done, but I'm having a bit of an issue with initializing a pointer for a structure. The code will compile, but it gives me an error saying that fileone is uninitialized. I've done some googling, and have found how to initialize a structure, but haven't had such luck for a pointer to a structure. Here's the relevant code:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <io.h>
#define len 256


typedef struct tree {
	char a[10][len];
};
tree* getFiles(int, char**);

tree* appendFiles(tree*);

int main(int argc, char ** argv) {


     tree*  fileList;

     fileList = getFiles(argc, argv); 
     appendFiles(fileList);      //appendFiles is another function that isn't relevant to the problem.
     return 0;
}

tree* getFiles(int unknown, char** location){
	int check=0, spot=0;
	tree* fileone;          // this seems to be the problem
	printf("Enter command line:");
	for(check; check!='\n'; spot++){
		scanf("%s", &fileone->a[spot]);
		check=fgetc(stdin);
	}
	printf("[creating output file '%s']\n", fileone->a[spot-1]);
	return fileone;
}



BTW, I know that I'm completely ignoring argv/argc which is passed to getFiles, but just ignore that(it's a requirement for the assignment that argv/argc be passed to getFiles, but we got behind and never discussed how to work with them).

Is This A Good Question/Topic? 0
  • +

Replies To: Initializing a pointer to a structure

#2 Guest_c.user*


Reputation:

Re: Initializing a pointer to a structure

Posted 05 December 2010 - 06:12 PM

you have to allocate memory for tree *fileone;
Was This Post Helpful? 1

#3 Bindayen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 10

Re: Initializing a pointer to a structure

Posted 07 December 2010 - 01:03 AM

View Postc.user, on 05 December 2010 - 05:12 PM, said:

you have to allocate memory for tree *fileone;


Thank you for the help, but now I'm getting a new error that I suspect might be related(or I could just be missing something incredibly obvious). In my program, it's supposed to support up to 10 files entered by a user, and I've got it working for multiple files right up until the return statement, but at that point it hits an error that says "Unhandled exception at 0x63ea89b9 (msvcr100d.dll) in notARealProject.exe: 0xC0000005: Access violation reading location 0x2e676573."

It also takes me to rmtmp.c. I honestly haven't the faintest why it's giving me this error. It works for entering 2 files, but 3 files gives me this error. I'd really appreciate a bit more help. Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <io.h>
#define len 2560


typedef struct tree {
	char a[10][len];
};
tree* getFiles(int, char**);

tree* appendFiles(tree*);

int main(int argc, char ** argv) {
//Local Variables\\


tree*  fileList;

fileList = getFiles(argc, argv); //Build the file list
appendFiles(fileList);
return 0;
}

tree* getFiles(int unknown, char** location){
	int check=0, spot=0;
	tree* fileone=(tree*)malloc(1000);
	printf("Enter command line:");
	for(check; check!='\n'; spot++){
		scanf("%s", &fileone->a[spot]);
		check=fgetc(stdin);
	}
	printf("[creating output file '%s']\n", fileone->a[spot-1]);
	return fileone;
}



tree* appendFiles(tree* filelist){
	int  size, counter=0, newLine=1, newLineCheck, lastletter='\n';
	char fpText[len], fullfpText[len]="";
	char otherfileText[len], fullotherfileText[len]="";
	tree* dummy=(tree*)malloc(256);
	FILE* fp;
	FILE* otherfile;
	FILE* newfile; 
	for(counter; counter<=10; counter++){
			fp=fopen(filelist->a[counter], "r");
			if(fp==NULL){
				fp=fopen(filelist->a[counter], "w");
				fputs(fullfpText, fp);
				printf("[done]\n");
				return filelist;    //here's where it breaks...
			}
			else{
				printf("adding %s", filelist->a[counter]);
			}
		fseek (fp, 0, SEEK_END);  //file size
		size = ftell (fp);
		rewind(fp);               
		printf("  (%d bytes, ", size);     //end file size
		newLine=1;
		for(newLineCheck=0; newLineCheck!=EOF;){   //number of lines
			newLineCheck = fgetc (fp);
			if (newLineCheck == '\n'){
				newLine++;
		  }
		}
		printf("%d lines)\n", newLine);	//end lines
		fseek(fp, 0, SEEK_SET);
			lastletter='\n';
		for(lastletter; lastletter=='\n' && lastletter!=EOF;){  
			fgets(fpText, len, fp);
			strcat(fullfpText, fpText);
			fseek(fp, -1, SEEK_CUR);
			lastletter=fgetc(fp);     
			//fseek(fp, -1, SEEK_CUR);
		}
		fclose(fp);
	}
	return filelist;
}



This post has been edited by Bindayen: 07 December 2010 - 01:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: Initializing a pointer to a structure

Posted 07 December 2010 - 01:38 AM

typedef struct tree {
    char a[10][len];
};



you've missed a new name for the type struct tree

typedef struct tree {
    char a[10][len];
} Tree;



so you make it
    Tree t;


the capital letter shows that this type name was defined by you

if this is working tree* fileList; then you are compiling in a C++ compiler

you don't do like this tree* fileone=(tree*)malloc(1000);
use this
    tree *fileone = (tree*) malloc(sizeof(tree));
    assert(fileone != NULL && "can't allocate");



you are writing check in the initialization block of the loop
for(check; check!='\n'; spot++){
write it like this
for( ; check != '\n'; spot++){

this thing
    scanf("%s", &fileone->a[spot]);
    check=fgetc(stdin);



    scanf("%s", fileone->a[spot]);
    check = getchar();



this wrong
for(counter; counter<=10; counter++){
for (counter = 0; counter < 10; counter++){
you initialized counter at the top, this is rather
and you have only ten lines in the structure, but with <= you are taking eleven

size = ftell (fp);
ftell() can be applyed only to files opened in the binary mode
Was This Post Helpful? 2

#5 Bindayen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 10

Re: Initializing a pointer to a structure

Posted 07 December 2010 - 11:23 AM

View Postc.user, on 07 December 2010 - 12:38 AM, said:

[very helpful and long post]
and you have only ten lines in the structure, but with <= you are taking eleven


About that, counter finds the element in the array that it's set to, and counter's initialized to 0, so it starts at filelist->a[0]. That leaves 10 spaces through filelist->a[9] for existing files, and filelist->a[10] for the desired name of the output file.

Thank you so much, though! I implemented the changes you suggested and it works just fine now, and I think you may have helped me on another program I'm working on. Thank you.

This post has been edited by Bindayen: 07 December 2010 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: Initializing a pointer to a structure

Posted 07 December 2010 - 12:20 PM

0 1 2 3 4 5 6 7 8 9 10 - count them, eleven times

and the structure has only ten 0 1 2 3 4 5 6 7 8 9
typedef struct tree {
    char a[10][len];
} Tree;


Was This Post Helpful? 1

#7 Bindayen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 10

Re: Initializing a pointer to a structure

Posted 07 December 2010 - 12:55 PM

View Postc.user, on 07 December 2010 - 11:20 AM, said:

0 1 2 3 4 5 6 7 8 9 10 - count them, eleven times

and the structure has only ten 0 1 2 3 4 5 6 7 8 9
typedef struct tree {
    char a[10][len];
} Tree;



Yep, you're right; I thought I had tested it with 10 files before I posted but I guess I only entered 9. /embarrassed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1