Re: char** function parameters



> Suppose I have the following function:
>
> void doSomething(char** array, int size);
>
> Normally, I would call the function with an array of, say, 3
> elements...
>
> char* strs[] = { "str1","str2","str3" };
> doSomething(strs, 3);

what this declaring is a pointer to an arrray. an array in memory is a
pointer to the data(i.e pointer to a pointer). The doSomething is asking for
a ponter to a pointer. which is way this one works

>
> That works fine. But if I want to pass it only one string, I would
> think this would work....
>
> char* str = "str1";
> doSomething(&str, 1);

while this declares a pointer to the data, doSomething is expecting a
pointer to a pointer and so it is treating "Str1" as the memory adress (i.e.
it is going to goto to adress 0x53747231) which is not where the string is.

>
> It compiles with a warning about incompatible pointer types, and
> needless to say it doesn't behave as I expected. "str1" does not get
> passed into the function. What am I doing wrong? Or is it simply not
> possible to pass a single char* into this function?
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.



Relevant Pages

  • Re: Malloc and recursion
    ... "sgi wrote:" above). ... how can a pointer be an index of an array? ... or is it the value pointed to by the pointer that is the index of the ... confusing world of newsgroups etc. can u be my guide here? ...
    (comp.lang.c)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)