
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