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
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.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
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; }
Last edited by MK27; 05-17-2009 at 11:26 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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++
Last edited by Panda_; 05-17-2009 at 11:29 AM.
> 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.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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: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;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:Originally Posted by Panda_
Out of curiosity, but what motivated you to ask this question?Code:*(&(a++));
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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);
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
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*.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".Originally Posted by BEN10
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)