Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


fprintf access problem

This topic is 5960 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

Hi everyone, I''m working on this tiny program trying to learn to use pointers with structures. In the program I am writing data from a structure to a file using fprintf. I am able to write one string and two ints to the file but when it tries to write one of two other strings it crashes with: some memory address referenced such and such memory address could not be read. The strings causing the error are identical as far as I can tell to the string that succeeds. Here is my header.
typedef struct{
	char *name;
	int age;
	char *city;
	char *state;
	int zip;
And here is my source file.
#include <stdio.h>
#include <stdlib.h>
#include "database.h"

int main(void)
	FILE *database;
	database = 0;
	employee emp1;

	emp1.name = emp1.city = emp1.state = 0;

	database = fopen("c:\\test.txt", "w+");
	if (database == 0)

	printf("%s", "\nEnter your name: ");
	emp1.name = (char *)malloc(20 * sizeof(char));	
	if (emp1.name == 0)
	scanf("%s", emp1.name);

	printf("%s", "\nEnter your age: ");
	scanf("%d", &emp1.age);

	printf("%s", "\nEnter your city: ");
	emp1.city = (char *)malloc(20 * sizeof(char));	
	if (emp1.city == 0)
	scanf("%s", &emp1.city);

	printf("%s", "\nEnter your state: ");
	emp1.state = (char *)malloc(20 * sizeof(char));	
	if (emp1.state == 0)
	scanf("%s", &emp1.state);

	printf("%s", "\nEnter your zip code: ");
	scanf("%d", &emp1.zip);

	fprintf(database, "%s%s\n", "Name: ", emp1.name);
	fprintf(database, "%s%d\n", "Age: ", emp1.age);
	fprintf(database, "%s%s\n", "City: ", emp1.city);  //Here is I get read error

	fprintf(database, "%s%s\n", "State: ", emp1.state); //I also get it here.

	fprintf(database, "%s%d\n", "Zip Code: ", emp1.zip);
When it writes the state or city I get the crash even when everything else is commented out. All the other stuff works when those lines are commented out. Can anyone see something that I might be missing?

Share this post

Link to post
Share on other sites
it may be that you are taking the address of something that is already a pointer. take the ''&'' off of the city and state members of the struct when you read them in, so for example it would look like:

instead of:

Share this post

Link to post
Share on other sites
of, if you dont want to take the addressof operator off, you can always read it in like


but the other way is used more often, and its a bit cleaner to read

Share this post

Link to post
Share on other sites
That did it, thanks!! But I am curious how does this change where it points to? i.e. isn''t that decided in the previous malloc assignment? Also why would this not effect the name pointer but only the city and state pointers, would it be order of allocation or their order in the structure? Still a little confused on it all, but thanks for showing me how to fix it!

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
It didn''t change what it points to, you just gave it a different address.
"ptr" is a variable that holds a value that is some address in memory

"*ptr" is the value stored at the memory location stored by "ptr"

"&ptr" is the address of the variable "ptr"

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!