(THE simplest question ever) char[] when the required argument is a char * 
Author Message
 (THE simplest question ever) char[] when the required argument is a char *

This is the simplest question ever ... however I've always had
problems with this issue.  (beginner programmer)

I have a function supplied by a 3rd party, I cannot change it.  The
arguments are like this:

void foo(char * string_ptr);

however for memory usage reasons I am using:
char string_tbl[1000];

How do I pass this "char string_tbl[1000]" to the foo function??

foo((char *)string_tbl); /* seems logical ... but does not compile*/

Anyone know how???  for now I thinking of pointing a char* on it but
there must be a smarter way to do this!

thanks!



Mon, 28 Jun 2004 10:18:57 GMT  
 (THE simplest question ever) char[] when the required argument is a char *
Quote:

> ...
> How do I pass this "char string_tbl[1000]" to the foo function??

> foo((char *)string_tbl); /* seems logical ... but does not compile*/

Firstly, is it not compiling or not linking?, because "a variable with
array type is considered a pointer to the type of the array elements".
So, there is no reason why the above line should refuse to compile,
unless it's been written in C++, in which case obviously, it won't
link.


Mon, 28 Jun 2004 16:26:47 GMT  
 (THE simplest question ever) char[] when the required argument is a char *

Quote:

> This is the simplest question ever

these are usually the {*filter*}es...

Quote:
> however I've always had problems with this issue.  (beginner programmer)

> I have a function supplied by a 3rd party, I cannot change it.  The
> arguments are like this:

> void foo(char * string_ptr);

> however for memory usage reasons I am using:
> char string_tbl[1000];

> How do I pass this "char string_tbl[1000]" to the foo function??

ah. C has some slightly tricky bits around here that trip beginners
(and not
so beginners). This works:-

     foo (string_tbl);

this is because an unadorned array name is a synonym (usually [1]) for
the
address of the first element, &string_tbl[0]. Which is what you want.
A good text book should explain this. For instance "The C Programming
Language" by
Kernighan and Richie (aka K&R). The comp.lang.c FAQ (Frequently Asked
Questions) also deals with pointers and arrays, it also has a
bibliography of
good C books (there are a lot of bad ones). The FAQ may be found at
    http://www.*-*-*.com/ ~scs/C-faq/top.html

Quote:
> foo((char *)string_tbl); /* seems logical ... but does not compile*/

> Anyone know how???  for now I thinking of pointing a char* on it but
> there must be a smarter way to do this!

[1] places it isn't include as a argument for sizeof()


Mon, 28 Jun 2004 18:05:41 GMT  
 (THE simplest question ever) char[] when the required argument is a char *
On 9 Jan, in article


Quote:
>This is the simplest question ever ... however I've always had
>problems with this issue.  (beginner programmer)

>I have a function supplied by a 3rd party, I cannot change it.  The
>arguments are like this:

>void foo(char * string_ptr);

>however for memory usage reasons I am using:
>char string_tbl[1000];

>How do I pass this "char string_tbl[1000]" to the foo function??

>foo((char *)string_tbl); /* seems logical ... but does not compile*/

You forgot to post the exact text of the error message.

That should be fine as long as the definition of string_tbl is
in scope. It is equivalent to the somple and more usual

 foo(string_tbl); /* seems logical ... but does not compile*/

Quote:
>Anyone know how???  for now I thinking of pointing a char* on it but
>there must be a smarter way to do this!

Using a pointer to the element type is the smart and normal way of
referencing an array. Since the code is failing to compile there
is presumably some problem with the code. Post a minimal piece of
code which you think should compile but doesn't.

--
-----------------------------------------


-----------------------------------------



Wed, 30 Jun 2004 23:28:12 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. A char pointer (char *) vs. char array question

2. recursively calling char *functions with char *arguments

3. Converting char array of literals chars to escape chars

4. char *c,char c[ ] and char *c[ ]

5. char, unsigned char, signed char

6. char* -> char (newbie question)

7. char[] and char * questions

8. a char and unsigned char question again

9. char* <> char* question

10. question: char* lltostr(long long, char*)?

11. char* -> char (newbie question)

12. Question: extern char * versus extern char[]

 

 
Powered by phpBB® Forum Software