Extremly sorry mate, i should have told you all in the begining that i'm using GCC compiler (fedora 14, linux).
And iMalc, i think the program in the above lastest post of mine will work fine (without errors). The problem here is not the "sizeof(void)" bt error comes from the printf line that i've commented.
And it would allocate the memory for void type succcessfully:-
Code:
for(i=0;i<4;i++) {
smith->ptr[i] = malloc(sizeof(void) * 5);
}
The error "invalid use of void expression" which the compiler is giving is for this line:-
Code:
printf("address of ptr[%d][%d] is %p and the value inside it is %d \n",i,j,&smith->ptr[i][j],(int)smith->ptr[i][j]);
If i try to print "sizeof(void)" it wont work, but how it is allocating memory "void" type in this line:-
Code:
smith->ptr[i] = malloc(sizeof(void) * 5);
I need a perfect reason for this.
Also i've done writting into void type allocated space, if you people are right then how this is working:-
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct me
{
void **ptr;
}*smith;
int main()
{
int i,j;
char buff[5]="hello";
smith = malloc(sizeof(struct me));
smith->ptr=malloc(sizeof(void *) * 4);
for(i=0;i<4;i++)
{
smith->ptr[i] = malloc(sizeof(void) * 5);
memset(smith->ptr[i],'\0',sizeof(void) * 5);
}
for(i=0;i<4;i++)
{
printf("address of ptr[%d] is %p and the value inside it is %p \n",i,&smith->ptr[i],smith->ptr[i]);
}
strncpy(smith->ptr[0],buff,5);
// or
// strcpy(&smith->ptr[0][0],buff);
printf("the string is : %s\n",smith->ptr[0]);
// or
// printf("the string is : %s\n",&smith->ptr[0][0]);
/* for(i=0;i<1;i++)
{
for(j=0;j<5;j++)
{
printf("address of ptr[%d][%d] is %p and the value inside it is %c \n",i,j,&smith->ptr[i][j],(char )smith->ptr[i][j]); ****Compiler give error on this line not on the sizeof(void)****
}
}
*/
return 0;
}
Thnk You
smith