Re: passing char arrays by reference



"jackassplus@xxxxxxxxx" <jackassplus@xxxxxxxxx> writes:
I'm a noob. Now that that is out of the way,
I want to pass an array of strings to a function:

int stuff(char some_string, int j){
strcpy("crap", some_string[j]);
j++;
return j;
}

int main(){
char a_string[10][10];
int i=0;
int result;
int j=0;
result = stuff(&a_string, &i);
printf("i=%i, j=%i, a_string=%s\n",i,j,a_string[i]);
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?

In a followup, you wrote:
sorry, just a clarification, the compiler tells me j is not an int.

Since j is an int, I seriously doubt that your compiler told you it
wasn't. It's always best to copy-and-paste the exact error message.
If you don't understand what it means, it's not likely you'll be able
to paraphrase it in a way that lets us know what it means.

Here's what I get when I compile your code with gcc:

c.c: In function 'stuff':
c.c:2: warning: incompatible implicit declaration of built-in function 'strcpy'
c.c:2: error: subscripted value is neither array nor pointer
c.c: In function 'main':
c.c:12: warning: passing argument 1 of 'stuff' makes integer from pointer without a cast
c.c:12: warning: passing argument 2 of 'stuff' makes integer from pointer without a cast
c.c:13: warning: incompatible implicit declaration of built-in function 'printf'

The first thing you need to do is add these lines:
#include <stdio.h>
#include <string.h>
<stdio.h> gives you the declaration of printf; <string.h> gives you
the declaration of strcpy. Your compiler might let you get away with
omitting the #include directives, but they're really not optional.

After adding those lines, I get:

c.c: In function 'stuff':
c.c:5: error: subscripted value is neither array nor pointer
c.c: In function 'main':
c.c:15: warning: passing argument 1 of 'stuff' makes integer from pointer without a cast
c.c:15: warning: passing argument 2 of 'stuff' makes integer from pointer without a cast

where line 5 is the call to strcpy and line 15 is the call to stuff.

You need to read the documentation for strcpy. You have the arguments
in the wrong order.

And your call to stuff just doesn't make any sense. You've declared
it to take two arguments of type char (that's a single character, not
what you want) and int, but you're passing arguments of types
char(*)[10][10] and int*.

So let's back off and take a look at what you're trying to do. You
say you want to pass an array of strings to a function. C has no
string type; a "string" is a data format, not a data type. If you
want to pass a string to a function, you'll usually pass a pointer to
a string (which is really just a pointer to the first character of the
string). For example:

void print_string(char *s)
{
printf("s = \"%s\""\n", s);
}

...

print_string("hello");

But you want to pass "an array of strings". Well, you can't do that
directly, but there are several ways to do it indirectly. One way to
represent an "array of strings" is as an array of pointers, each of
which is of type char*. Each element of this array points to a
string.

Or you can have a two-dimensional array of char (or, equivalently, an
array of array of char), as you've tried to do here. Each row of the
array is an array of char which can contain a string. If you want to
pass one of these strings to print_string(), for example, you need to
obtain its address.

I suggest reading sections 4, 6, and 8 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) kst@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: Dynamic lists of strings in C
    ... Probably because it's rarely needed and there's too much memory ... It's much better to always use size_t instead of unsigned int, ... isn't a NULL pointer. ... Why assign an empty string to the "undefined" elements in between ...
    (comp.lang.c)