Hallo Guys
I have some basic questions about C Programming, 'cause i'm not very good in it.
Why is it impossible for an Array-initializing function to do the following ?
Thanks for your help.Code:int[] init() { return int[32]; }
Greez
Printable View
Hallo Guys
I have some basic questions about C Programming, 'cause i'm not very good in it.
Why is it impossible for an Array-initializing function to do the following ?
Thanks for your help.Code:int[] init() { return int[32]; }
Greez
You can't return an array, you can only return a pointer.
Also, you can't return a pointer to a variable that is local to the function, since that variable disappears when the function ends.
It's not impossible, it's just a matter of doing it properly. Your first problem is basic syntax, and your second is that you need to allocate heap memory for the array -- you cannot/should not return a "local" stack variable as those only last as long as the function call. So:
Code:#include <stdlib.h> /* for malloc() */
#include <stdio.h> /* for printf() */
int *rayfunc() {int *this=malloc(sizeof(int)*32); return this; }
int main() {
int *ray=rayfunc(), i;
ray[31]=13;
for (i=0;i<32;i++) printf("%d\n",ray[i]);
free(ray); /* deallocate memory */
return 0;
}
Thanks.
It's never ever possible to return an array?
And another question:
Why do the following ones do not return the same result?
1. int *a = address; a++
2. char *a = address; a++
> It's never ever possible to return an array?
You shouldn't need to return an array anyway since arrays can be accessed by other functions than the one the array is declared in by passing pointers. Try to design your functions to accept a pointer to the first element as an argument, instead.
> Why do the following ones do not return the same result?
The char type is smaller than int, so ++ moves the address by a different number of bytes.
Thanks guys for your help.
If you don't mind there's one more question:
which type do variables from
"int * a,b"
have?
Nope, because that would have to be a stack variable (get it?). You can only return a single value, either an address (ie a pointer, like to an array) or plain int/char/datatype.
So one thing you can do to get around this is typedef a custom datatype:
However, the typedef has to be a struct or the array rules apply anyway.Code:#include <stdio.h>
typedef struct {
int ray[32];
} intray;
intray rayfunc() {
intray this = {{0}};
this.ray[31]=13;
return this;
}
int main() {
int i;
intray example=rayfunc();
for (i=0;i<32;i++) printf("%d\n",example.ray[i]);
return 0;
}
Also, if you want to pass an existing array to a function for modification, use a pointer.
It would be wise to ask such questions with a code snippet posted in bbcode tags. If you are talking about the type of a and of b as declared by this line of code:Quote:
Originally Posted by Panda_
then the answer is that a is of type int* (pointer to int) and b is of type int. Because of a potential misinterpretation (one could accidentally or ignorantly consider b as being of type int*, I try to declare variables separately, especially if a pointer is involved.Code:int * a,b;
Ok i got it.... thanks a lot.
Does anybody know in which order this expression gets evaluated?
Code:*&a++
With some exceptions, the order of evaluation of an expression is unspecified (but in this case there is nothing to vary). If we talk about the grouping of operators with respect to precedence, then it would be:Quote:
Originally Posted by Panda_
Out of curiosity, but what motivated you to ask this question?Code:*(&(a++));
I dont understand what is the problem in evaluating *&a++; As i see it, '*' and '++' has the same priority, with RtoL associativity thus a++ will be done first and as a result the whole expression should give the value at address '&a'. But when i run this code it doesn't even compiles. Why is it so?
Error 1 error C2102: '&' requires l-valueCode:#include<stdio.h>
int main(void)
{
int a=10;
printf("%d",*&a++);
}
When i tried to print
it gave the answer 10.Code:printf("%d",*&a);
You can't take the address of a++, seeing it is not stored in memory anywhere. (Edit: If a is a pointer, and a++ is valid to dereference, you should be able to do &*a++.)
Postfix operator++ has a higher precedence than unary operator*.Quote:
Originally Posted by BEN10
As I illustrated and as tabstop implied, the expression dereferences the address of the result of a++, but the result of a++ is an rvalue, which is why your compiler complained that "'&' requires l-value".Quote:
Originally Posted by BEN10