const char* to char[64] 
Author Message
 const char* to char[64]

My complier (g++) is giving me:
"incompatible types in assignment of const char* to char[64]"

Why? How is this differing from :

char[64] = "hello";

Thanks for any help in advance!

rt



Sun, 12 Aug 2001 03:00:00 GMT  
 const char* to char[64]
Hard to tell without more code to work with. But I'm /guessing/ that you
have tried something like this:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char array[64] = "hello"; /* in your example, you missed out the
variable name. I'm presuming that's a typo. */

    printf("array has [%s] in it right now.\n", array);

    array = "hi there";    /* WRONG */

    printf("array now has [%s] in it.\n", array);

    return EXIT_SUCCESS;

Quote:
}

The line marked /* WRONG */ should read

strcpy(array, "hi there");

strcpy prototype is in string.h.

That's my psychic research for the day finished. Now to get on with some
levitation.

--
Richard H

#include "sig.h"



Quote:
> My complier (g++) is giving me:
> "incompatible types in assignment of const char* to char[64]"

> Why? How is this differing from :

> char[64] = "hello";

> Thanks for any help in advance!

> rt



Sun, 12 Aug 2001 03:00:00 GMT  
 const char* to char[64]

Quote:
>My complier (g++)

g++ is a C++ compiler, so comp.lang.c++ might have been a more
appropriate forum.

Quote:
>                  is giving me:
>"incompatible types in assignment of const char* to char[64]"

>Why?

Presumably, you have something like

  char carray[64];
  ...
  carray = "hello";

You can't assign to arrays, but you can assign to their constituent
elements. So

  int iarray[64];
  iarray = 3;     /* assignment to array, not allowed */

is illegal, but

  int iarray[64];
  iarray[0] = 3;  /* assignment to element, allowed */

is fine.

Quote:
>How is this differing from :

>char[64] = "hello";

If you mean something like

  char carray[64] = "hello";

the difference is that this is an initialisation, not an assignment.
As its name suggests, initialisation sets the _initial_ value for an
object.

  int i = 5; /* defines i, and initialises it to 5 */
  int j;     /* defines j, but does not initialise it */

  j = 3;     /* assigns 3 to j (not an initialisation) */

Other types of array can be initialised too, e.g.

  int iarray[64] = { 1, 2, 3, 4, 5 };

but, as with character arrays, assignment

  iarray = { 1, 2, 3, 4, 5 };

is illegal.

-- Mat.



Sun, 12 Aug 2001 03:00:00 GMT  
 const char* to char[64]

Quote:

>My complier (g++) is giving me:
>"incompatible types in assignment of const char* to char[64]"
>Why? How is this differing from :
>char[64] = "hello";

I assume you wanted to type

   char a[64] = "hello";

and the identifier got lost somewhere on the way from your brain to my
newsreader. This is an initialization, and it is just a shorthand form
for writing

   char a[64] = { 'h', 'e', 'l', 'l', 'o' };

The rest of the array of 64 char will be initialized with '\0' characters.
It is _not_ an assignment. In C, assignments to arrays are not allowed.
You might want to read the section on "Arrays and Pointers" in the FAQ for
comp.lang.c

Kurt

--
| Kurt Watzka                            



Sun, 12 Aug 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

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

2. const char * vs char const *

3. const char *p == char const *p ?

4. char** and const char** ...

5. va_arg(ap, const char *) = char*?

6. char^ to const char* casting

7. const char ** incompatible with char ** (and vice versa)?

8. passing const char* to char* fails

9. const char* vs. char *

10. converting const char* to unsigned char[1024]

11. char *'s and const char *'s

12. const char* to char* ?

 

 
Powered by phpBB® Forum Software