typedef struct{
	char *name;
	bool exists_in_sys;
	convert_table *data;
} unit;

float  orig_quant;
unit *orig_unit = (unit*) malloc(sizeof(unit));
unit *new_unit = (unit*) malloc(sizeof(unit));

printf("Enter original quantity, original units, new units\n");
fscanf(stdin, "%f %s %s", &orig_quant, orig_unit->name, new_unit->name);

printf("Original unit: %s\n", orig_unit->name);
printf("New unit: %s\n",2) new_unit->name);

OK, so I would like to know:

1) Why the fscanf string assignment doesn't work... the last 2 printf's output
Original unit: (null)
New Unit: (null)

Isn't an fscanf string assignment supposed to work on a char pointer (which is what orig_unit->name is)?

2) Is there any way to manipulate the format characters after the format string? For example, if I could do something like

fscanf(stdin, "%s", strcpy(%s, other_string));

Thanks for any help or comments

You are allocating memory for the structure, but not for name element of that structure and that's why fscanf() doesn't work.

>Isn't an fscanf string assignment supposed to work on a char pointer (which is what orig_unit->name is)?
Yes, but pointers aren't magic. You can't simply define a pointer and expect it to point to an infinite amount of usable memory, you must first point it to a block of memory owned by your process for the purpose of storing a string:

orig_unit->name = malloc(MAX_NAME);
new_unit->name = malloc(MAX_NAME);

/* ... */

fscanf(stdin, "%f %s %s", &orig_quant, orig_unit->name, new_unit->name);

>Is there any way to manipulate the format characters after the format string?
I don't understand the question. Could you please rephrase it?

Thanks for your help, I forgot that allocating memory to the structure doesn't allocate memory to its fields. Nevermind about my second question, now that I think about it, it doesn't make much sense.

Thanks again,
Ross

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.