0

I have 2 pointers declared like this:

void *arr1,*arr2;

I'm going to dynamically allocate memory for these 2 pointers and then scan some input to them. For instance, for the 1st one i do this:

scanf("%c",&typ);
scanf("%d",&len1);

The user enters the type of the input, and the size he wishes for the "array". My question is how to scan items to these arrays using scanf function. I tried this:

/*scan 1st array items*/
if(typ == 'i')
{
    arr1 = (int*)malloc(len1 * sizeof(int));
    for(i=0 ; i < len1 ; i++)
        scanf("%d",arr1[i]);
}
if(typ == 'f')
{
    arr1 = (float*)malloc(len1 * sizeof(float));
    for(i=0 ; i < len1 ; i++)
        scanf("%f",arr1[i]);
}
if(typ == 'c')
{
    arr1 = (char*)malloc(len1 * sizeof(char));
    for(i=0 ; i < len1 ; i++)
        scanf("%c",arr1[i]);
}
/*end scanning 1st array*/

But i get these errors:

errors

What am I doing wrong? Thank you

Community
  • 1
  • 1
Alaa M.
  • 4,961
  • 10
  • 54
  • 95
  • 1
    Please post text, rather than images of text... – Oliver Charlesworth Jun 01 '13 at 18:28
  • Your scanf("..", arr1[i]) blows up, because you can't index into an "array" of void. Think about it `arr1[i]` is equivalent to `arr1 + i * sizeof(void)`. But what exactly is `sizeof(void)`? That expression doesn't make any sense. – djf Jun 01 '13 at 19:15
  • @djf you're right. how about `scanf("%d",arr1+i*sizeof(int));` for the 1st if condition? why doesn't it work? – Alaa M. Jun 01 '13 at 19:21
  • 3
    [You can't do pointer arithmetic on a void pointer](http://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c). You have to cast the whole thing to (int*), then index into it and finally pass a pointer to _scanf_. Something like &(((int*)arr1)[i])... but that's horribly messy - I feel dirty even talking about it :) I would prefer Binayaka Chakraborty's approach if you absolutely have to store different types into a contiguous memory block – djf Jun 01 '13 at 19:27

2 Answers2

4

If you are trying to do what I think you are trying to do, you are better off with defining a union, then using it to define variable use.

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

typedef union s
{
    int d;
    char c;
    float f;
}multi;

int main()
{
    multi *p;
    int len;
    char type;
    int loop;
    printf("Enter the type::\t");
    scanf("%c",&type);
    printf("Enter the length::\t");
    scanf("%d",&len);
    if(type == 'i')
    {
        p = malloc (sizeof(multi) * len);
        for(loop=0;loop<len;loop++)
        {
            scanf("%d",&p[loop].d);
        }
        printf("Values::\n");
        for(loop=0;loop<len;loop++)
        {
            printf("%d\n",p[loop].d);
        }
    }
    else if (type == 'f')
    {

        p = malloc (sizeof(multi) * len);
        for(loop=0;loop<len;loop++)
        {
            scanf("%f",&p[loop].f);
        }
        printf("Values::\n");
        for(loop=0;loop<len;loop++)
        {
            printf("%f\n",p[loop].f);
        }
    }
    else if (type == 'c')
    {

        p = malloc (sizeof(multi) * len);
        for(loop=0;loop<len;loop++)
        {
            scanf("%c",&p[loop].c);
        }
        printf("Values::\n");
        for(loop=0;loop<len;loop++)
        {
            printf("%c\n",p[loop].c);
        }
    }
    else
    {
        printf("\nNot a valid type!\n");
    }
    free(p);
    return 0;
}

The reason is that with a union the extra space is not wasted, while you get the flexibility of using a pointer to access the variables. It is not advisable to cast malloc() type

0

change to

if(typ == 'i')
{
    arr1 = malloc(len1 * sizeof(int));
    for(i=0 ; i < len1 ; i++)
        scanf(" %d", (int*)arr1 + i);
}
if(typ == 'f')
{
    arr1 = malloc(len1 * sizeof(float));
    for(i=0 ; i < len1 ; i++)
        scanf(" %f", (float*)arr1 +i);
}
if(typ == 'c')
{
    arr1 = malloc(len1 * sizeof(char));
    for(i=0 ; i < len1 ; i++)
        scanf(" %c", (char*)arr1 +i);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70