Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a simple newbie question in C

Reply
Thread Tools

a simple newbie question in C

 
 
diffuser78@gmail.com
Guest
Posts: n/a
 
      02-11-2006
I want to return an array from a function and catch it in an an array
variable.

For example like this

int main(){
int a[3];
a = get_value();
return 0;
}

int[] get_value(){
int b[3]={0,1,2};
return b;
}


This is a bit like Java. How to get this thing done in C.

Please help

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-11-2006
wrote:
> I want to return an array from a function and catch it in an an array
> variable.
>
> For example like this
>
> int main(){
> int a[3];
> a = get_value();
> return 0;
> }
>
> int[] get_value(){
> int b[3]={0,1,2};
> return b;
> }
>
>
> This is a bit like Java. How to get this thing done in C.


C is not Java, and one of the differences (there are
many) is in the way arrays are implemented. It is sometimes
said that arrays are not "first-class citizens" in C, mostly
because there are only two operators that work on entire
arrays: the `sizeof' operator and the `&' address-of operator.
In particular, the `=' assignment operator doesn't work on
arrays, so your attempt at `a = get_value()' is doomed from
the start.

What to do instead? Well, it depends on what you want
to accomplish. Take a step back, realize that you're not
writing Java, and don't let the syntactic similarity of C
and Java sucker you into trying to use Java techniques in a
C program. "Gift" is a perfectly good noun in both English
and German (closely-related languages), but while you might
give one to a friend you would not give it to a Freund.

So: What's the larger problem you're trying to solve?

--
Eric Sosman
lid
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      02-11-2006
"Eric Sosman" writes:

>> I want to return an array from a function and catch it in an an array
>> variable.
>>
>> For example like this
>>
>> int main(){
>> int a[3];
>> a = get_value();
>> return 0;
>> }
>>
>> int[] get_value(){
>> int b[3]={0,1,2};
>> return b;
>> }
>>
>>
>> This is a bit like Java. How to get this thing done in C.

>
> C is not Java, and one of the differences (there are
> many) is in the way arrays are implemented. It is sometimes
> said that arrays are not "first-class citizens" in C, mostly
> because there are only two operators that work on entire
> arrays: the `sizeof' operator and the `&' address-of operator.
> In particular, the `=' assignment operator doesn't work on
> arrays, so your attempt at `a = get_value()' is doomed from
> the start.
>
> What to do instead? Well, it depends on what you want
> to accomplish. Take a step back, realize that you're not
> writing Java, and don't let the syntactic similarity of C
> and Java sucker you into trying to use Java techniques in a
> C program. "Gift" is a perfectly good noun in both English
> and German (closely-related languages), but while you might
> give one to a friend you would not give it to a Freund.
>
> So: What's the larger problem you're trying to solve?


I wasn't going to reply, but since I don't give a good God damn what the
larger problem you are trying to solve is, herewith my reply.

void fill(int b[])
{
for(int i=0; i<3; i++)
b[i] = i;
}
int main()
{
int a[3];
fill(a);
return 0;
}
----
There is nothing to return, a is modified in place.



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-11-2006
writes:
> I want to return an array from a function and catch it in an an array
> variable.
>
> For example like this
>
> int main(){
> int a[3];
> a = get_value();
> return 0;
> }
>
> int[] get_value(){
> int b[3]={0,1,2};
> return b;
> }
>
>
> This is a bit like Java. How to get this thing done in C.


Array types are second-class citizens in C. You can't assign them or
return them from functions. You need to use pointers.

If you have a C textbook, read the sections on arrays and pointers.
If you don't, get one; I recommend Kernigan & Ritchie, _The C
Programming Language_, Second Edition.

See also section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      02-13-2006
osmium wrote:
[snip]
> void fill(int b[])
> {
> for(int i=0; i<3; i++)
> b[i] = i;
> }


Or better:

void fill(int b[], int len)
{
for (int i = 0; i < len; i++)
b[i] = i;
}


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
al3x4nder@gmail.com
Guest
Posts: n/a
 
      02-14-2006
hi, all!
i think, you can write something, like that:

int * get_array(int size){
int i, *ptr = malloc(sizeof(int) * size); /* memory allocate*/
for(i = 0; i < size; i++){
ptr[i] = i; /* fill array*/
}
return ptr; /* return pointer to array */
}

and from main it look like that:
int i, size = 10, * arr = get_array(size);
for(i = 0; i < size; i++){
printf("%d\n", arr[i]);
}

p.s sorry for my english
------------------------
best regards
Alexander Chugaevsky
icq: 314285577

 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      02-14-2006
wrote:
> hi, all!
> i think, you can write something, like that:
>
> int * get_array(int size){
> int i, *ptr = malloc(sizeof(int) * size); /* memory allocate*/
> for(i = 0; i < size; i++){
> ptr[i] = i; /* fill array*/
> }
> return ptr; /* return pointer to array */
> }
>
> and from main it look like that:
> int i, size = 10, * arr = get_array(size);
> for(i = 0; i < size; i++){
> printf("%d\n", arr[i]);
> }


It is more flexible to let the client supply the buffer to be filled.

> p.s sorry for my english


No problem, but correct capitalization will certainly help.


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
Frodo Baggins
Guest
Posts: n/a
 
      02-14-2006
wrote:
> hi, all!
> i think, you can write something, like that:
>
> int * get_array(int size){
> int i, *ptr = malloc(sizeof(int) * size); /* memory allocate*/
> for(i = 0; i < size; i++){
> ptr[i] = i; /* fill array*/
> }
> return ptr; /* return pointer to array */
> }
>
> and from main it look like that:
> int i, size = 10, * arr = get_array(size);
> for(i = 0; i < size; i++){
> printf("%d\n", arr[i]);
> }
>
> p.s sorry for my english
> ------------------------
> best regards
> Alexander Chugaevsky
> icq: 314285577


Hi
Please don't forget to clean up by free()ing your malloc()ed array.
Regards,
Frodo Drogo Baggins

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57