2

I have this code

#include <stdio.h>

int main(void)
{
    char cad[] = "abc";

    char (*ptr)[1];

    ptr[0] = cad;

    return 0;
}

When compiled throws me this error:

error #2168: Operands of '=' have incompatible types 'char [1]' and 'char *'.

Why this error occurs?

12
  • ptr[0] is a single character ... cad is a pointer .. Commented May 16, 2016 at 16:53
  • 2
    Could you clarify which part of the error you don't understand? Commented May 16, 2016 at 16:53
  • What are you trying to do? you should write ptr[0]=cad[0] Commented May 16, 2016 at 16:53
  • as should be the allocation Commented May 16, 2016 at 16:57
  • The error from clang is "test.c:9:12: error: array type 'char [1]' is not assignable". The compiler thinks ptr[0] is an array (because you told it so). Therefore, ptr[0] cannot be on the left side of an assignment operator. Commented May 16, 2016 at 17:00

4 Answers 4

3

Why this error occurs?

Expression ptr[0] has type char[1] because ptr is declared lika pointer to array of type char[1]

char (*ptr)[1];

Expression cad has type char * and is equal to the address of the first character of array cad.

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

Thus in the left side of the assignment statement

ptr[0] = cad;

there is an array of type char[1]. In the right side of the assignment there is a pointer of type char *. These types are incompatible and arrays do not have the assignment operator.

It seems you mean the following

#include <stdio.h>

int main(void)
{
    char cad[] = "abc";

    char * ptr[1];

    ptr[0] = cad;

    // puts( ptr[0] );

    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@AniMenon Try to uncomment the commented statement.
1

I think this is what you want to do :

#include <stdio.h>

int main(void)
{
    char cad[] = "abc";
    int n = sizeof (cad) / sizeof (cad[0]);
    char (*ptr)[n];
    ptr = &cad;
    printf("%s",*ptr);
    return 0;
}

Output:

abc

The problem with what you were doing was :

char cad[] = "abc";
char (*ptr)[1];
ptr[0] = cad;

char array cad has "abc", pointer array of type char and then you were trying to assign a single index of the pointer array(which is a char) with a char[](that is cad), hence causing a type mismatch.

Comments

1

Why this error occurs (sic)?

char cad[] = "abc";

Here, cad is a char[4] (containing characters 'a', 'b', 'c', '\0'.)

char (*ptr)[1];

and here, ptr is a pointer to char[1]. So ptr[0] is a char[1].

In the assignment expression

 ptr[0] = cad;

cad decays to char*. So you end up with incompatible operands, namely char[1] on the LHS and char* on the RHS, just like the compiler error message tells you.

Two further things worth mentioning:

  • Arrays are not assignable, so there is no assignment expression for which ptr[0] = ???; is valid, given the type of ptr.

  • There are no arrays of pointers in your code.

Comments

0

The error message from the compiler is very clear.

type of ptr is char (*)[1].
type of ptr[0] is char [1].
cad decays to a pointer when used in ptr[0] = cad. Hence the type of the RHS is char*. You cannot assign a char* to a char [1].

Also,

ptr[0] = ...;

leads to undefined behavior since memory has not been allocated for ptr.

It's unclear what you are trying to accomplish. You can do something like:

char (*ptr)[1] = malloc(sizeof(*ptr));
ptr[0][0] = cad[0];

This will put the first character of cad in ptr[0][0]. However, ptr[0] is not a null terminated string. Hence, you won't be able to use it like a string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.