Re: Can array[]=malloc()ed?
From: Mantorok Redgormor (nethlek_at_tokyo.com)
Date: 10/11/03
- Next message: pete: "Re: Avoid having to press Enter"
- Previous message: chad kline: "Re: piping/reading stdin quirks."
- In reply to: Eric Sosman: "Re: Can array[]=malloc()ed?"
- Next in thread: Mark McIntyre: "Re: Can array[]=malloc()ed?"
- Reply: Mark McIntyre: "Re: Can array[]=malloc()ed?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: pete: "Re: Avoid having to press Enter"
- Previous message: chad kline: "Re: piping/reading stdin quirks."
- In reply to: Eric Sosman: "Re: Can array[]=malloc()ed?"
- Next in thread: Mark McIntyre: "Re: Can array[]=malloc()ed?"
- Reply: Mark McIntyre: "Re: Can array[]=malloc()ed?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|