Re: Can array[]=malloc()ed?

From: Mantorok Redgormor (nethlek_at_tokyo.com)
Date: 10/11/03


Date: 10 Oct 2003 19:34:46 -0700

Eric Sosman <Eric.Sosman@sun.com> wrote in message news:<3F86DF34.2F36D936@sun.com>...
> dam_fool_2003@yahoo.com wrote:
> >
> > Friends,
> > cannot we malloc a array?
> > So, I tried the following code:
> >
> > int main(void)
> > {
> > unsigned int y[3]={1,3,6},i,j;
> > for(i=0;i<3;i++)
> > printf("before =%d\n",y[i]);
> > *y = 7; /* 1*/
> > for(j=0;j<3;j++)
> > printf("after =%d\n",y[j]);
> > return 0;
> > }
> >
> > In the above I derefered *y in the line indicated by1 (correct me if I
> > have used a wrong term since pointers are derefered in this way. But
> > in the above I have used array but derefered as a pointer since
> > an array always decays into pointers.) the array's first element value
> > changes. Considering this as a kind of proof that *y pointes to the
> > first element in a array I wanted to malloc the array y. I tried
> > various syntax but I only get warnings.
> > *y = malloc(sizeof y);
>
> If `y' is still declared as shown above, `*y' is an `int'.
> malloc() does not return an `int' value; malloc() returns a
> pointer. You cannot store a pointer value in an `int' variable.

You can't? What about the following:

int foo, *ptr;

ptr = &foo;
foo = (int)ptr;

>
> > In spite of the warnings I have compiled the code and the code I not
> > have any run time error.
>
> After giving you the diagnostic, the compiler apparently
> tried to help you by altering your code to a correct form,
> perhaps something like `*y = (int)malloc(sizeof y)'. If this
> is what happened, the result was to forcibly convert the pointer
> returned by malloc() into a corresponding `int' value, and store
> that converted value in `*y'. The conversion might or might not
> make sense; it might not even produce a legitimate `int' value;
> your program might perfectly well explode at this point -- or it
> might appear to do something sensible. The code as it stands is
> incorrect, and there's really no telling what it might do.
>
> > Can somebody tell me the correct syntax?
>
> No, because you haven't explained what you are trying to
> do. The compiler (apparently) chose a correction in an attempt
> to get some sense out of your code, but I am just as ignorant
> as the compiler about your true intention, and my "correction"
> is no more likely to be the right one than the compiler's was.
> Neither of us can read your mind.
>
> > By the malloc of above the array's first element's mem get
> > allocated, Am I correct?
>
> I'm sorry: I cannot understand this question at all.
>
> > What happens I we use a multi dimensional array?
>
> Everything depends on how you use it.
>
> Your understanding of arrays and of memory allocation seems
> to be incomplete. I recommend that you study Sections 6 and 7
> in the comp.lang.c Frequently Asked Questions (FAQ) list
>
> http://www.eskimo.com/~scs/C-faq/top.html
>
> Sections 4 and 5 might also be helpful. Good luck!

- nethlek



Relevant Pages

  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Newbie
    ... to talk about the int value 3 and the int value 4, ... It also lets you talk about pointer ... C has a special rule for array objects. ... to printf() is: ...
    (comp.lang.c)
  • Re: allocate 2d array in function and return it to caller
    ... > data into an 2d array, and passes that array back to the caller ... Even better would be, rather than the for each pointer code, allocate one ... you only have two calls to malloc rather than ROW+1. ... > int create2DimArray(int nx, ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)