I was up coding at 3 AM last night, and I wake up today and find this in a source file: (curse words redacted)
void append_this_stuff(char *stuff_to_append_to[], char **stuff_to_append, int position) {
char the_actual_stuff[] = *(stuff_to_append_to);
char *screw_me = *(stuff_to_append);
int someNumber = strlen(screw_me);
int j = 0;
for (int i = position; i < (someNumber + position - 1); i++) {
the_actual_stuff[i] = (screw_me + j);
j++;
}
stuff_to_append_to = &the_actual_stuff;
}
When I try to compile it, I get this error:
<project root>/src/brstring.c: In function ‘append_this_stuff’:
<project root>/src/brstring.c:38:28: error: invalid initializer
char the_actual_stuff[] = *(stuff_to_append_to);
^
<project root>/src/brstring.c:46:24: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
the_actual_stuff[i] = (screw_me + j);
^
<project root>/src/brstring.c:50:21: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
stuff_to_append_to = &the_actual_stuff;
Does anyone have any idea if I'm doing this right? I'm compiling via the C99 standard and cmake and I'm using GCC on Fedora Linux, should that affect anything.
First, char *stuff_to_append_to[]
is an array of pointers of undetermined length, that is not a valid parameter, as the last dimension of an array must be specified when passed to a function, otherwise, pass a pointer to type.
Next, char **stuff_to_append
is a pointer to pointer to char and is completely valid, but given your use of stuff_to_append
within the function, it is obvious that is not what you intended.
If you wish to insert stuff_to_append
and truncate stuff_to_append_to
at the end of stuff_to_append
simply pass a pointer to each string as the argument. While int position
is fine, a choice of an unsigned value may be better as you will not insert at a negative array index.
Inside your function, you must validate there is sufficient space in stuff_to_append_to
to hold stuff_to_append
beginning at index position
(including space for the nul-byte)
With that in mind, you may want to do something like the following:
void append_this_stuff (char *stuff_to_append_to, char *stuff_to_append,
int position)
{
int somenumber = strlen (stuff_to_append),
lento = strlen (stuff_to_append_to),
end = position + somenumber;
if (end > lento) {
fprintf (stderr, "error: insufficient space in stuff_to_append_to.\n");
return;
}
for (int i = position; i < end + 1; i++) /* +1 to force copy of nul-byte */
stuff_to_append_to[i] = stuff_to_append[i - position];
}
You can write a small test program to confirm it's operation, e.g.
#include <stdio.h>
#include <string.h>
...
int main (void) {
char stuff[] = "my dog has fleas!",
append[] = "cat has none!";
int pos = 3;
printf ("original: %s\n", stuff);
append_this_stuff (stuff, append, pos);
printf (" new: %s\n", stuff);
return 0;
}
Example Use/Output
$ ./bin/append
original: my dog has fleas!
new: my cat has none!
To do the same thing using pointer arithmetic instead of using array indexes, you can rewrite append_this_stuff
similar to the following:
void ats (char *to, char *from, int p)
{
if (p + strlen (from) > strlen (to)) {
fprintf (stderr, "error: insufficient space in stuff_to_append_to.\n");
return;
}
for (to += p; *from; to++, from++)
*to = *from;
*to = *from;
}
Lastly, if this lesson is not completely ingrained in your thought process, "Never post anything that you would not want in the hands of the recruiter as you interview for your first programming position." The use of unprofessional or cute variable names, while it may express your frustrations, may not give the impression you want in the hands of another. Enough said.
1.) error: invalid initializer:
char the_actual_stuff[] = *(stuff_to_append_to);
The_actual_stuff is an array without an initial size. Did you mean to use a pointer to point to a memory address?
2.) warning: assignment makes integer from pointer without a cast:
the_actual_stuff[i] = (screw_me + j);
You are trying to assign a value to an array index that has no initial size.
A side note: if you meant to add the values of the variables together you must first dereference the pointer with a *. You are adding an integer to a memory address.
3.) warning: assignment from incompatible pointer type
stuff_to_append_to = &the_actual_stuff;
See this post: as it explains it nicely.