cannot convert from char* to char[4] 
Author Message
 cannot convert from char* to char[4]

I get this error when i try to allocate memory to a pointer to an
array of char. sample code......

#include<stdio.h>

#define ROWS 3

int main(void)
{
        char (*s)[4];
        int i,j;

        *s = (char*)malloc(ROWS * 4 * sizeof(char));
        for(i=0; i<ROWS; i++)
        {
                printf("\nEnter value\t\t");
                for(j=0; j<4; j++)
                        scanf("%c", (*(s+i)+j));
        }
        for(i=0;i<3; i++)
                printf("%s", *(s+i));
        return 0;

Quote:
}

kindly help.

Thanks,
chithra



Mon, 11 Jul 2005 01:07:24 GMT  
 cannot convert from char* to char[4]

Quote:
> I get this error when i try to allocate memory to a pointer to an
> array of char. sample code......
> #include<stdio.h>

You also need #include <stdlib.h> for malloc().

Quote:
> #define ROWS 3
> int main(void)
> {
>    char (*s)[4];
>    int i,j;
>    *s = (char*)malloc(ROWS * 4 * sizeof(char));

Lose the cast. It's so pointless it wouldn't get a point from
Points-R-Us from Pointsville.
You're trying to assign a value of type (char *) to a variable of type
(char (*)[4]). How do you expect this to work? malloc() returns a
(void *), which is compatible with ANY object pointer type. So you can
assume that the compiler knows what the right type is and you don't need
to tell it.
(Note that in C++ it's a different story. C++ abandons this perfectly
good idea of pointer compatibility.)

Quote:
>    for(i=0; i<ROWS; i++)
>    {
>            printf("\nEnter value\t\t");
>            for(j=0; j<4; j++)
>                    scanf("%c", (*(s+i)+j));

You can also write the second argument as s[i]+j.

Quote:
>    }
>    for(i=0;i<3; i++)
>            printf("%s", *(s+i));

You can also write the second argument as s[i].

Note that since there is no terminating '\0' character in the
memory pointed to by s, printf() will cause undefined behaviour by
{*filter*}ling on to unallocated memory.

Quote:
>    return 0;
> }
> kindly help.
> Thanks,
> chithra

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.*-*-*.com/ ~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow e{*filter*}dly zooming."
   - JIPsoft


Mon, 11 Jul 2005 01:33:40 GMT  
 cannot convert from char* to char[4]


Quote:
> I get this error when i try to allocate memory to a pointer to an
> array of char. sample code......

> #include<stdio.h>

Need to include <stdlib.h>

Quote:

> #define ROWS 3

> int main(void)
> {
> char (*s)[4];
> int i,j;

> *s = (char*)malloc(ROWS * 4 * sizeof(char));

To avoid type incompatibility,
s=malloc(ROWS * 4 * sizeof(s));
Quote:
> for(i=0; i<ROWS; i++)
> {
> printf("\nEnter value\t\t");
> for(j=0; j<4; j++)
> scanf("%c", (*(s+i)+j));
> }
> for(i=0;i<3; i++)
> printf("%s", *(s+i));
> return 0;
> }

> kindly help.

> Thanks,
> chithra



Mon, 11 Jul 2005 01:35:48 GMT  
 cannot convert from char* to char[4]

Quote:


>> I get this error when i try to allocate memory to a pointer to an
>> array of char. sample code......

>> #include<stdio.h>
> Need to include <stdlib.h>
>> #define ROWS 3

>> int main(void)
>> {
>> char (*s)[4];
>> int i,j;

>> *s = (char*)malloc(ROWS * 4 * sizeof(char));
> To avoid type incompatibility,
> s=malloc(ROWS * 4 * sizeof(s));

ITYM s=malloc(ROWS * sizeof(*s));

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"We sorcerers don't like to eat our words, so to say."
   - Sparrowhawk



Mon, 11 Jul 2005 01:40:50 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Converting char array of literals chars to escape chars

2. Cannot convert from class CEdit to const char *

3. Convert char* to char __gc[]

4. Convert four chars to a long and a long to four chars

5. Converting unsigned char to signed char invokes UB?

6. converting char to pointer char

7. Convert an 8bit char in a 7bit char!

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

9. Convert from char to unsigned char

10. convert char to const char

11. Any way to convert char (*) [256] to char * []?

12. converting a char* to char[10]

 

 
Powered by phpBB® Forum Software