Reputation: 233
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?
Upvotes: 1
Views: 910
Reputation: 18522
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>
like strchr
and memcpy
.
Here's an alternative working example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *extract_string(char *str, char delim)
{
size_t len;
char *new_str;
char *delim_pos = strchr(str, delim);
/* new string is the length from the start of the old string to the
* delimiter, or if it doesn't exist, a copy of the whole string */
if (delim_pos == NULL)
return strdup(str);
len = delim_pos - str;
new_str = malloc(len + 1);
memcpy(new_str, str, len);
new_str[len] = '\0'; /* NUL terminate the new string */
return new_str;
}
int main(void)
{
char *extracted1 = extract_string("some:string", ':');
char *extracted2 = extract_string("no delimiter", ':');
puts(extracted1);
puts(extracted2);
/* free the dynamically allocated buffers */
free(extracted1);
free(extracted2);
return 0;
}
Output:
some
no delimiter
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 strtok
to extract tokens.
Upvotes: 4
Reputation: 436
The problem is (*labelname)++. You are incrementing the value that's pointed to by labelname and simultaneously assigning to it the value that's pointed to by currentline. If you want to increment the pointers, use *labelname++ and *currentline++
Upvotes: 0