that's not the assignment operator, that's just the syntax of initialization.
that's not the assignment operator, that's just the syntax of initialization.
Could you elaborate on that, please? I'm not quite following you.
Initialize:
Assign:Code:int bob[] = {2,3,4};
The first makes sense, the second does not -- assignment can only happen element by element, not to the array-as-a-whole. Arrays are passed to function by pointer -- so your array[5][11] is passed as a pointer to an array of arrays of size 11, and we can assign a pointer value to a pointer variable no problem:Code:int bob[3]; bob = {2,3,4};
Indexing on ptr will work the same as indexing on array.Code:int array[5][11]; int (*ptr)[11]; //a pointer to array(s) of 11 integers; ptr = array; //wonderful
here's the BNF for declarations in N1256 (C99 + TC1 + TC2 + TC3):there's a = character there, but it's not the assignment operator. it's just a = character.Code:declaration: declaration-specifiers init-declarator-listopt ; declaration-specifiers: storage-class-specifier declaration-specifiersopt type-specifier declaration-specifiersopt type-qualifier declaration-specifiersopt function-specifier declaration-specifiersopt init-declarator-list: init-declarator init-declarator-list ,init-declarator init-declarator: declarator declarator =initializer
Last edited by robwhit; 09-05-2009 at 04:54 PM. Reason: quoted the initialization section instead of the declaration section. same concept, but not what the discussion was about.
Ok, so in C, when we have the follwing code
then when "testfnc" is called, the variable "b" is transfered to "testfnc" byCode:void testfnc(int a) { printf("%d", a); } int main() { int b = 5; testfnc(b); return 0; }
and NOTCode:int a; a=b;
?Code:int a=b;
I would say that the (formal) parameter a is initialised to be a copy of the (actual) argument b. There is no assignment involved, at least not in the way that has been mentioned in this thread.Originally Posted by Niels_M
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
In the context of C (and not C++ classes) the two examples are essentially identical (compiler implementation details aside, at least). Some memory is set aside for the variable and then the data is copied to that address. Simple as that. As to why you can't assign to an array what you can initialize it with, well, that's just the way C works. And just to be clear, in this case:
The array is not copied, just a pointer to it.Code:void f(int (*a)[5])
Copied, converted - what's the distinction being made there?
I have a final question. We have the following code
Here "test" will point at "one", and "test+1" will point at "two".Code:int main() { char* test[4] = {"one", "two"}; return 0; }
When I write "*test", then I access the string "one", thus "*(*test+1)" will give us the value 'n'.
When I say "access", then what is it that we are doing? I think it has to do with pointers to pointers, but I cannot see how.
More accurately, test is an array of pointers to char, not a pointer. test+1 is a pointer, and it points to a pointer to the first character of "two".Originally Posted by Niels_M
In the expression *test, test is converted to a pointer to its first element, i.e., a pointer to a pointer to char. Hence, *test is a pointer to a char, in particular, a pointer to the first character of "one". Hence, (*test+1) is a pointer to the second character of "one", thus *(*test+1) results in the value of the second character of "one", i.e., 'n'.Originally Posted by Niels_M
By the way, since string literals should not be modified, you should declare test to be an array of const char* instead.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Maybe this will make it more clear:
Code:int main( void ) { typedef size_t* address; char const * one = "one", * two = "two", * array[ ] = { one, two }; cout << "Address of '&array[ 0 ]': " << address( array ) << endl; cout << "Address of '&array[ 1 ]': " << address( array + 1 ) << endl; cout << "Address of 'one': " << address( one ) << endl; cout << "Address of 'two': " << address( two ) << endl; cout << "Value of 'array[ 0 ]': " << address( array[ 0 ] ) << endl; cout << "Value of 'array[ 1 ]': " << address( array[ 1 ] ) << endl; return 0; }
Yes, but isn't that basically saying the same thing? I'm not trying to be difficult here - I just don't see any difference, really.