Does anyone know how to test the structural equivalence of an array in C?
I'm unsure if I should be tested the size of the array. Or the 1st element of the array (b/c the name is equivalent to a pointer to the first element).
Does anyone know how to test the structural equivalence of an array in C?
I'm unsure if I should be tested the size of the array. Or the 1st element of the array (b/c the name is equivalent to a pointer to the first element).
What's the "structural equivalence of an array"? You cannot know, given just a memory address, if it is part of an allocated array, or just a single variable, or if you can even dereference that point in memory.
Quzah.
Hope is the first step on the road to disappointment.
this is what lead me to the possibility of using the sizeof(). Very confusing question to say the least..
homework - Are two int arrays of different size "type equivalent" in C? - Stack Overflow
Would strcmp help at all?
http://linux.die.net/man/3/strcmp
Originally Posted by The Jargon File
You still aren't telling us in plain English what it is you're trying to do.
Quzah.
Hope is the first step on the road to disappointment.
"That array constructor in C does not construct a new type and structural equivalence is applied to arrays. How can your write code to test this? i.e. how can you test that C uses structural equivalence between arrays?"
verbatim.
from my understanding arr[0-9] is structurally equivalent to arr[1-10]. so maybe I should just be checking the type of used within the array and the count of primitive types in the array.
Last edited by DJPlayer; 02-26-2011 at 03:02 AM.
I still don't know what it is you are trying to prove. You're trying to prove what? That two different sized arrays of the same element type are the same? How can you expect anyone to be able to help you if you can't put in simple clear terms what it is you think you're trying to prove? One more time:
What is "structural equivalence"? - In your own words.
Quzah.
Hope is the first step on the road to disappointment.
The single equals sign is an assignment a = b; You can assign any element of an array using indexing such as a[17] = b; but you cannot assign whole arrays.
The double equals sign is a comparison if (a == b) you can test individual array elements using this such as if (a[17] == b) but you cannot test whole arrays.
Why not? Because an array's name is a pointer. It's just a pointer to the first element of the array. What you end up doing is assigning one array's pointer to the other's address... leaking memory in the process.
As a C programmer you are not working with managed code, there are no constructors or destructors, there's no garbage collector... it's 100% entirely up to you to keep track of what is what and how big it is, and to write code that does not leak memory, violate array boundaries, try to assign 200 bytes to an integer and so on... That's the power of this language and it's biggest weakness.
This still doesn't explain what "structural equivalence" is. What's a "structure"? Obviously not a C struct, so what? Do you mean "identically the same in memory"? No, of course it wouldn't be the same - why would you expect an array that is size X to be the same thing as an array that is size Y?
An array isn't a type specifier. It's a quantity specifier.
Quzah.
Last edited by quzah; 02-26-2011 at 05:32 AM.
Hope is the first step on the road to disappointment.
I didn't realize there was a memory leak involved in the mere use of the pointer in C. But essentially your statement of how C looks at arrays was going to be my attempt. The fact that you have a pointer to the first element obviously says something about it's structure. The funny thing about structure is that the bounds are not part of it. Which is why I stated arr1[0-9] is structurally equivalent to arr2[1-10]. We have to arrays of integers both w/ 10 elements both with integers.. now this is vs. name equivalence where C looks at the name to make a determination.. used in primitive types.
essentially I'm trying to find a way to say these two arrays are structurally equivalent.
Code:int x[] = { [0 ... 9] = 1 }; int y[] = { [1 ... 10] = 1 };
Last edited by DJPlayer; 02-26-2011 at 11:48 AM.