const char *' differs in levels of indirection from 'char 
Author Message
 const char *' differs in levels of indirection from 'char

I keep seeing questions regarding chars and strings. Now I have my own.....
The following snipet of code generates the error below.
I looked up the error and the explanation indicates that I have mismatching
types.
But I can't figure out how to correct the discrepancy.
I am trying to remove 5 characters from input_line (some code that tests if
I have
the right character is missing for the sake of simplicity)  and concatenate
them
one at a time into temp.
If I printf ptr[i] in the loop, it displays the correct character from
input_line.
Any ideas, or if you can point me in the right direction.....?
Thanks

char *ptr;
char temp[81];

ptr = &input_line[0];
strncpy(temp, ptr, 1);
for (i = 0; i < 6; i++)
      strncat(temp, ptr[i], 1);  <-----Generates error below on compile

-------------------Configuration: farebl - Win32 Debug--------------------
Compiling...
farebl.c
C:\farebl.c(211) : warning C4047: 'function' : 'const char *' differs in
levels of indirection from 'char '
C:\farebl.c(211) : warning C4024: 'strncat' : different types for formal and
actual parameter 2

Farebl.obj - 0 error(s), 2 warning(s)

--



Fri, 29 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char
On Mon, 11 Oct 1999 19:52:02 GMT, in comp.lang.c.moderated "The

[...]

Quote:
>for (i = 0; i < 6; i++)
>      strncat(temp, ptr[i], 1);  <-----Generates error below on compile

>-------------------Configuration: farebl - Win32 Debug--------------------
>Compiling...
>farebl.c
>C:\farebl.c(211) : warning C4047: 'function' : 'const char *' differs in
>levels of indirection from 'char '
>C:\farebl.c(211) : warning C4024: 'strncat' : different types for formal and
>actual parameter 2

Let's see if we can figure this out...

The first warning says that (const char *) has a different level of
indirection than (char).  Indirection means pointers.  Specifically,
the warning says that (const char *) points to something, but (char)
does not.  OK so far?

The second warning says that the required (formal) type of the second
parameter of strncat is not the type you are providing (the actual
type).  If you look at the prototype for strncat, you'll see the type
of formal parameter 2 is (const char *).  Sound familiar?

So we see that the second parameter to strncat must be a pointer, but
ptr[i] is not a pointer.  In fact, ptr[i] has type (char).  Sound
familiar?  I'll leave fixing the error as an exercise...

Regards,

                          -=Dave
Just my (10-010) cents
I can barely speak for myself, so I certainly can't speak for B-Tree.
Change is inevitable.  Progress is not.
--



Sat, 30 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char


Quote:
>If I printf ptr[i] in the loop, it displays the correct character from
>input_line.
>Any ideas, or if you can point me in the right direction.....?
>Thanks

>char *ptr;
>char temp[81];

>ptr = &input_line[0];
>strncpy(temp, ptr, 1);
>for (i = 0; i < 6; i++)
>      strncat(temp, ptr[i], 1);  <-----Generates error below on compile

Your code is a little odd, and I doubt that it will do what you want but
the error is that strncat takes two pointers and a size_t (usually that
is an unsigned int).  What you gave was a pointer (temp) a char (ptr[i])
and an int.  The first and last of these will work fine but the middle
one won't.  You should have written ptr+i to get an address.

But why didn't you write:

strncat(temp, ptr, 6);

?

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sat, 30 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char
: I keep seeing questions regarding chars and strings. Now I have my
: own.....  The following snipet of code generates the error below.
: I looked up the error and the explanation indicates that I have
: mismatching types.  But I can't figure out how to correct the discrepancy.
: I am trying to remove 5 characters from input_line (some code that tests
: if I have the right character is missing for the sake of simplicity)
: and concatenate them one at a time into temp.

[ ... ]

: char *ptr;
: char temp[81];
:
: ptr = &input_line[0];
: strncpy(temp, ptr, 1);
: for (i = 0; i < 6; i++)
:       strncat(temp, ptr[i], 1);  <-----Generates error below on compile

The second argument of strncat should be a const char*, and you
give it a char.  I suppose you need `ptr + i' instead of `ptr[i]'.

Eric

--

 Eindhoven Univ. of Technology
 Lab. for Catalysis and Inorg. Chem. (SKA)
--



Sat, 30 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char

strncat(temp, &ptr[i], 1);

Quote:

>I keep seeing questions regarding chars and strings. Now I have my own.....
>The following snipet of code generates the error below.
>I looked up the error and the explanation indicates that I have mismatching
>types.
>But I can't figure out how to correct the discrepancy.
>I am trying to remove 5 characters from input_line (some code that tests if
>I have
>the right character is missing for the sake of simplicity)  and concatenate
>them
>one at a time into temp.
>If I printf ptr[i] in the loop, it displays the correct character from
>input_line.
>Any ideas, or if you can point me in the right direction.....?
>Thanks

>char *ptr;
>char temp[81];

>ptr = &input_line[0];
>strncpy(temp, ptr, 1);
>for (i = 0; i < 6; i++)
>      strncat(temp, ptr[i], 1);  <-----Generates error below on compile

>-------------------Configuration: farebl - Win32 Debug--------------------
>Compiling...
>farebl.c
>C:\farebl.c(211) : warning C4047: 'function' : 'const char *' differs in
>levels of indirection from 'char '
>C:\farebl.c(211) : warning C4024: 'strncat' : different types for formal and
>actual parameter 2

>Farebl.obj - 0 error(s), 2 warning(s)

--
*/ unmatched closing comment
--



Sat, 30 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char


Quote:
>I keep seeing questions regarding chars and strings. Now I have my own.....
>The following snipet of code generates the error below.
>I looked up the error and the explanation indicates that I have mismatching
>types.
>But I can't figure out how to correct the discrepancy.
>I am trying to remove 5 characters from input_line (some code that tests if
>I have
>the right character is missing for the sake of simplicity)  and concatenate
>them
>one at a time into temp.
>If I printf ptr[i] in the loop, it displays the correct character from
>input_line.
>Any ideas, or if you can point me in the right direction.....?
>Thanks

>char *ptr;
>char temp[81];

>ptr = &input_line[0];
>strncpy(temp, ptr, 1);
>for (i = 0; i < 6; i++)
>      strncat(temp, ptr[i], 1);  <-----Generates error below on compile

This code differs from your description of it. Here you try to copy
the first 6 characters from the inputline to temp.
You said you wanted to delete the first 5 characters from the
inputline.
This will do what you want:

char temp[81] = { 0 }; /* initialize temp to all zero's */

strncpy(temp, &inputline[5], 80); /* copy a maximum of 80 char's from
inputline, starting at index 5 (character 6), to temp */

- Show quoted text -

Quote:

>-------------------Configuration: farebl - Win32 Debug--------------------
>Compiling...
>farebl.c
>C:\farebl.c(211) : warning C4047: 'function' : 'const char *' differs in
>levels of indirection from 'char '
>C:\farebl.c(211) : warning C4024: 'strncat' : different types for formal and
>actual parameter 2

>Farebl.obj - 0 error(s), 2 warning(s)

>--


Bart v Ingen Schenau
--



Sat, 30 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char
Hi!

[...]

Quote:
> char *ptr;
> char temp[81];

> ptr = &input_line[0];
> strncpy(temp, ptr, 1);
> for (i = 0; i < 6; i++)
>       strncat(temp, ptr[i], 1);  <-----Generates error below on compile
[...]
> C:\farebl.c(211) : warning C4047: 'function' : 'const char *' differs in
> levels of indirection from 'char '

strncat appends no more than n bytes of one string to the end of another,
i.e. it expects two strings (char * or char [ ]).

ptr being a char *, ptr[i] is a char, so not what is expected...

You can remove your for () and put:

strncat (temp, ptr, 6) ;

And this should do.

CUX

--



Sat, 30 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char

Quote:

> char *ptr;
> char temp[81];
> ptr = &input_line[0];
> strncpy(temp, ptr, 1);
> for (i = 0; i < 6; i++)
>       strncat(temp, ptr[i], 1);  <-----Generates error below on compile

A character is not a C string; for one thing, it has no null terminator.
Although your approach could be fixed up (using &ptr[i] for example),
it is inefficient and you still need to terminate the resulting string.
char *p = strchr(temp, '\0');   /* find end */
for (i = 0; i < 6; ++i)
        p[i] = ptr[i];
p[i] = '\0';
--



Sat, 30 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char
In your example, ptr is a 'char *', when you derefernce it (i.e. ptr[i])
you 'go' one level up and get a char.

strncat() expect a pointer to char but you are attenpting to pass it a
char.

C does not have strings. Only pointers to char. That might seem pedantic,
but it isn't really.

 As an aside, your code below is probably not what you want to do:
how about:
        strncpy(temp, ptr, 6);

Regards,
Mitch



Quote:
> I keep seeing questions regarding chars and strings. Now I have my
own.....
> The following snipet of code generates the error below.
> I looked up the error and the explanation indicates that I have
mismatching
> types.
> But I can't figure out how to correct the discrepancy.
> I am trying to remove 5 characters from input_line (some code that tests
if
> I have
> the right character is missing for the sake of simplicity)  and
concatenate
> them
> one at a time into temp.
> If I printf ptr[i] in the loop, it displays the correct character from
> input_line.
> Any ideas, or if you can point me in the right direction.....?
> Thanks

> char *ptr;
> char temp[81];

> ptr = &input_line[0];
> strncpy(temp, ptr, 1);
> for (i = 0; i < 6; i++)
>       strncat(temp, ptr[i], 1);  <-----Generates error below on compile

> -------------------Configuration: farebl - Win32

Debug--------------------
Quote:
> Compiling...
> farebl.c
> C:\farebl.c(211) : warning C4047: 'function' : 'const char *' differs in
> levels of indirection from 'char '
> C:\farebl.c(211) : warning C4024: 'strncat' : different types for formal
and
> actual parameter 2

> Farebl.obj - 0 error(s), 2 warning(s)

> --


--



Sat, 30 Mar 2002 03:00:00 GMT  
 const char *' differs in levels of indirection from 'char

Quote:

> I keep seeing questions regarding chars and strings. Now I have my own.....
> The following snipet of code generates the error below.
> I looked up the error and the explanation indicates that I have mismatching
> types.
> But I can't figure out how to correct the discrepancy.
> I am trying to remove 5 characters from input_line (some code that tests if
> I have
> the right character is missing for the sake of simplicity)  and concatenate
> them
> one at a time into temp.
> If I printf ptr[i] in the loop, it displays the correct character from
> input_line.
> Any ideas, or if you can point me in the right direction.....?
> Thanks

> char *ptr;
> char temp[81];

> ptr = &input_line[0];
> strncpy(temp, ptr, 1);
> for (i = 0; i < 6; i++)
>       strncat(temp, ptr[i], 1);  <-----Generates error below on compile

> -------------------Configuration: farebl - Win32 Debug--------------------
> Compiling...
> farebl.c
> C:\farebl.c(211) : warning C4047: 'function' : 'const char *' differs in
> levels of indirection from 'char '
> C:\farebl.c(211) : warning C4024: 'strncat' : different types for formal and
> actual parameter 2

The error is that the second parameter to strncat() should be const
char *, that is a pointer to a char.

In your code, "ptr" is a pointer to a char, but ptr[i] is not a
pointer, it is the char at index i.  Perhaps (ptr + i) is what you
want.

strncat also appends a NUL terminator.  Is that OK?

You want to copy 5 characters.  How many does you program actually
copy?  Does it copy the character at index 0 twice?

The usual C code for copying characters (not strings) looks something
like:

for (i = 0 , ...., i++) {
        *destination++ = *source++;

Quote:
}

See also the function memcpy(), which is very useful for copying
characters.

--

--



Sun, 31 Mar 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. char *'s and const char *'s

2. what's the difference between char * and const char *

3. How to convert from 'unsigned char *const ' to 'const char *',

4. Help: inline char const* const& max(char const* const &a, char const* const &b)

5. casting 'char **' to 'const char**'

6. const char * vs char const *

7. const char *p == char const *p ?

8. const char *'s and Wwrite-strings

9. strchr and const char *'s

10. what's differences between char [] and char* ?

11. Lookin' for char *GetFileDate(char *pFileName)

12. Can't insert char into CHAR column

 

 
Powered by phpBB® Forum Software