I’m posting this as a vent for my questions (I will have a multitude). I decided it would be rather annoying to keep asking the same person one question at a time (said person is very busy), so I’ll be posting questions as I come across them in my project. If you feel like helping, please do, and I would greatly appreciate it!
Note: this means I’ll be updating this post frequently. Help is greatly, greatly appreciated as always.
EDIT so you guys want me to just keep posting different questions if I come across them? Of course I always do research before asking you guys, you talented group of men and women only get the most persistent of errors.
My first question:
I keep getting the error:
lvalue required as left operand of assignment
THE PURPOSE of this code is to copy the first n character up to ':'. For instance, if currentline is: "LABEL: .long 100" then GetLabelName would return "LABEL".
NOTE strncpy isn’t working for this. It returns the remaining characters after ignoring the first n characters instead of just returning the first n characters…
Here’s the code that’s causing the error:
char *GetLabelName(char *currentline){
char *labelname[200];
while((((*labelname)++)=(*currentline)++)!=':');
return labelname;
}
Something is fishy about this code I guess, but I can’t figure out what. Any ideas?
What I think you’re trying to do is extract/copy all of the characters in a string up until a certain point (
':'or NUL) and return that buffer. If that’s the case, you’re going to need to dynamically allocate memory for the new string (you can’t return a local buffer allocated on the stack), and you should also take advantage of functions in<string.h>likestrchrandmemcpy.Here’s an alternative working example:
Output:
If you don’t want to make a copy when the delimiter isn’t found, you could alternatively return
NULL.Alternatively, if you don’t mind mangling your initial string, you could use
strtokto extract tokens.