I'm a noob. Now that that is out of the way,
I want to pass an array of strings to a function:
How you do it depends on how the array of strings is defined. Two
obvious possibilities come to mind immediately:
1 - An array of arrays of char. Given appropriate values for
M and N, the array
char array_of_arrays[M][N];
can hold up to M strings, each of which can have a length between 0
and N-1, inclusive.
2 - An array of pointers to char. Given an appropriate value
for M, the array
char *array_of_pointers[M];
can hold up to M pointers to char, each of which can point to a
string. The declaration does not place any limit on the length of the
strings.
Your code below has problems.
int stuff(char some_string, int j){
strcpy("crap", some_string[j]);
Multiple problems here:
some_string is a char so you cannot apply a subscript to it.
If you change the type of some_string so that it does contain
or point to a string, then some_string[j] would be a char and that is
not a suitable second argument for strcpy.
If you change the type of some_string so that it does contain
an array of strings, your call to strcpy would attempt to replace the
string literal "crap" with the contents of the j-th string. Attempting
to modify a string literal invokes undefined behavior.
j++;
return j;
}
int main(){
char a_string[10][10];
The contents of a_string are uninitialized and therefore
indeterminate. Attempts in stuff to copy from this indeterminate data
do not produce well defined results. If there is no '\0' in the data,
strcpy will write all over memory, invoking undefined behavior. Even
if there is a '\0' in the data before strcpy overflows the target
location, you have no idea what data was copied.
int i=0;
int result;
int j=0;
result = stuff(&a_string, &i);
More problems here:
You are passing the address of a char[10][10] array to stuff.
For this to work, you need to declare the parameter in the definition
of stuff as char(*some_string)[10][10] and pass it as the second
argument to strcpy as (*some_string)[j].
This is unnecessarily cumbersome. If you remove the & from
the argument, you are passing the array to stuff. In most contexts
(use with the & operator as above being one of three exceptions), an
expression with array type is converted to the address of the first
element with type pointer to element type. In this case, the
expression a_string would be converted to &a_string[0]. You can then
declare the parameter in stuff as either char (*some_string)[10] or
the easier, to my mind, char some_string[10][10]. (Note: some prefer
the former because it more closely matches what is actually passed to
the function.) In either case, you pass it as the second argument to
strcpy simply as some_string[j].
You are also passing the address of i, which has type int*,
when the parameter is expecting an int. Since you use the int
directly in stuff, the obvious solution is to remove the &.
printf("i=%i, j=%i, a_string=%s\n",i,j,a_string);
While attempting to copy from a_string produces random or chaotic
results, attempting to print from it produces formally undefined
behavior.
return 0;
}
the compiler tells me that j is not a string, and that argument 1 of
stuff is an incompatible pointer type. Why is that?
You passed a char(*)[10][10] to a function that was expecting a simple
char. As noted, argument 2 is incompatible also.