No. What you're not saying is that your trick --and it is a trick-- only works for static types. I saw the declaration and that is why I pointed it out.
Without the static modifier the array is disposed of, released from the stack, the instant the program returns... any pointer retuned to that variable is immediately invalid and very likely to produce incorrect results.
But you are NOT returning the array... C can't do that. What you are doing is what I said; returning a pointer and choosing to treat it as an array.No, it is an array.
It is clearly declared as an array.
Yes they will. But unless you tell them the limitations and pitfalls of of the scheme you're using, you are indeed misleading them about what the language can and cannot do.All that is clearly indicated within the code.
I'm not keeping any secrets here.
The beginner is going to learn about scope eventually.
Yes that's part of it... but even a static array is an entity of unknown size. The language doesn't have a list someplace that says "this array is this big, that array is that big"... that information is simply not stored anywhere. You can declare a static int array of 3 elements inside your function, return a pointer to it then treat that pointer as a pointer to a 2 dimensional array of 30 x 50... neither the the compiler nor the runtime will know or care. Thus it is up to you to treat these things correctly. In the end it has very little to do with CPU registers and a lot more to do with careful programming, which these guys won't do unless they have appropriate information... Telling them C can return arrays because you know a trick that makes it look that way is simply wrong.No, actually, its because an array doesn't fit into the EAX register, but, a pointer to the array does.
But, that's being too technical, isn't it.
Nobody said any such thing... do not put words in my mouth to further your own arguments.No, actually Tater, I did think about it. And I knew you would bring it up.
I'm well aware of the consequences of static variables.
However, they are an established part of the C programming language.
And, nearly all programming languages.
You cannot ignore that they do exist.
Nobody said it was a bad feature... you're putting words in my mouth again!Explain then why the text books and documentation go to lengths to describe the usage of these "bad" features.
Why then, are they part of the ANSI Standard ?
What I did say is that showing them stuff like this without explaining why and how it works is likely to lead to them misunderstanding how the C language actually works.
What trick ??
Nowhere is "static" equated with "trick".Originally Posted by Wikipedia
Because that's how you do it, Tater.But you are NOT returning the array... C can't do that. What you are doing is what I said; returning a pointer and choosing to treat it as an array.
What are you referring to here ??Nobody said any such thing... do not put words in my mouth to further your own arguments.
Maybe...by your response ...? I don't know...Nobody said it was a bad feature... you're putting words in my mouth again!
Well, actually no... because that's how the LANGUAGE does it.
Like you said, you can't return an array because it won't fit in the register.
Ok... since you seem far more interested in the argument than the facts... one more try at this before I ignore you...
I did not say that what you are doing is wrong. I've used it myself, when it's appropriate. ... ok, got that?
What I did point out is that although it appears to return an array, all it's really doing is returning a pointer that you are choosing to treat as an array in your code. The point was not what you are doing... the point was that by not providing an explaination of how it works, people will mistakenly believe that C can return an actual array and until they learn about static modifiers and rules of scope, your unexplained trick is likely to cause them some misunderstaning of how C actually works... Get it? I didn't say your code was wrong... you simply should have explained it far enough to avoid misunderstandings.
Or in more direct terms... Don't be telling people C does stuff it does not know how to do.
(oh yeah... and welcome to my ignore list!)
???...listen...
is that the sound of a bicycle being peddled backwards ?
Yes, you can do it but I think you're bringing up something irrelevant. It's funny that you say beginners don't care about semantics, but you won't admit what's factually true about the examples you post. I think beginners should care about semantics (it's not like they go away), and I'm not going to say something wrong just because you want to say you can return arrays.
There are problems with the static array approach "We" don't "do it all the time" because:
- it's not thread safe
- you will be recycling the same array for each invocation of the function
- therefore, copy the pointed-to data if you want to keep it
- you will be returning a pointer to that array's first element, which should not be freed
If the programmer cares about any one of these at least, then static is not the answer.
But I've been up front before in saying that my answer is only one way to get around the return value semantics. I mentioned that you could use multiple pointers, instead of an array. It just seemed to be the one NinjaFish was interested in. I'm sorry.
Last edited by whiteflags; 04-18-2011 at 04:44 PM.
Why using static arrays and returning pointers is a very bad idea...
As I said in my original post about this... What happens if you call the function twice?Code:#include <stdio.h> int* MyFunction(int a, int b, int c) { static int array[3]; array[0] = a; array[1] = b; array[2] = c; return array; } // return a pointer. int main (void) { int *a1, *a2; // int pointers printf("calling a1 = MyFunction(10,20,30);\t"); a1 = MyFunction(10,20,30); printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]); printf("calling a2 = MyFunction(100,200,300);\t"); a2 = MyFunction(100,200,300); printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]); printf("\nLooks good, except...\t"); printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]); getchar(); return 0; }
I'm thinking our friend Steve A who "does this all the time" just might have some serious debugging to do...
Last edited by CommonTater; 04-18-2011 at 07:03 PM.
Why does everything here (this forum) have to turn into a pi_ss_ing contest ?Originally Posted by CommonTater
Tater, you and I both know your example is from page-1, chapter-1, of Pointers-101.
Everyone who learns about pointers learns about the side-effects of using them. Big-wup.
Anybody can take just about any C method and illustrate a worst case senario.
Your example is simply that.
Here I thought that maybe you really had something to show us.
I'm a bit disappointed that you didn't have something more interesting.
Just because I don't share the same religious ferver about C that you do, it doesn't automatically make me wrong and like wise the opposite.
We simply have differing opinions on certain subjects.
It's my belief that a student of C should be exposed to every aspect of the language and not be told that "no you can't use that, because I don't like it".
Your example could be better served, instead, by saying: "if you do decide to use pointers to return values, be aware of the side-effects" and then show your example. Just like the textbooks do on the very same subject.
Pretty simple really... you posted bad advice, didn't explain the pitfalls... You tried to tell people C can do something it cannot.
You got tagged for it.
THEN you decided to push the argument, even to the point of claiming I'd said things I did not.
It's a contest because you made it into one.
Even now, when there is proof of your misstatements right before your eyes... you STILL persist in trying to be right.
It's kinda sad really... My better half, outspoken lady she is, would call it "testosterone poisoning".
Really.. you screwed up... admit it and move on.
(With God as my witness, I promise you, your testicles will not fall off if you admit an error)
Last edited by CommonTater; 04-19-2011 at 08:59 AM.
You didn't: the fact is that one cannot return an array in C. It is possible to return a pointer to the first element of an array and thereby simulate returning an array, but it is not the same thing. It is possible to return a struct object that contains an array, but it is not the same thing.Originally Posted by Steve A.
Next time, illustrate your point instead of illustrating something else.Originally Posted by Steve A.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Edit: I missed seeing laserlight post; need to refresh more often.
Steve A: In the C Language Arrays are treated as Pointers when passed on the stack.
Recognize that fact; then think about what CommonTater said. He is much more right than you are.
Tim S.
Last edited by stahta01; 04-19-2011 at 09:48 AM. Reason: Grammer
C DOES NOT return arrays. It cannot because it does not know their size. You used a clever programming trick to make it seem like it does... and I provided a simple, clear, example of why it's a bad idea to use your trick in real programs. Simply because returning pointers couples all arrays back to the static one in the function... use that thing twice and you end up with array values that change "by magic" whenever you call the function. On it's surface it looks real clever... in reality it's a patently stupid thing to do unless you know exactly the consequences of doing it... which you either did not or could not explain. Frankly I'm guessing you discovered this trick of yours and thought yourself ever so clever, remaining blissfully unaware of it's consequences.
No sir... I have no turf. Such stupid notions are the premise of an immature mind.More like I got flamed for treading on your turf.