a simple newbie question in C

D

diffuser78

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
 
E

Eric Sosman

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?
 
O

osmium

Eric Sosman said:
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;
}
int main()
{
int a[3];
fill(a);
return 0;
}
 
K

Keith Thompson

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/>.
 
A

August Karlstrom

osmium wrote:
[snip]
void fill(int b[])
{
for(int i=0; i<3; i++)
b = i;
}


Or better:

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


August
 
A

al3x4nder

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; /* 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);
}

p.s sorry for my english
 
A

August Karlstrom

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; /* 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);
}


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
 
F

Frodo Baggins

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; /* 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);
}

p.s sorry for my english


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top