Question:
Why an array index always starts with zero in C?
2009-09-16 02:06:45 UTC
1. Why an array index always starts with zero in C?
2. In "void main(int argc, char *argv)" why arg is a vector pointer? why cant it be simply a string?
3. Please tell me with an example diff. between is-a and has-a type of inheritance?
Please kindly, you can tell answer to all or any of 3.
Four answers:
2009-09-16 11:58:32 UTC
1) as the other's explained it because arrays are pointers and you are giving offsets from the base pointer



2) because there are multiple strings - c breaks up all the options on the command line so you don't have to.



3) is-a represents that an object is of this type. For example, a circle is a geometric shape, as is a square and rectangle. has-a indicates containment or ownership. For example, students have classes, or a restaurant has customers, meals, etc.. In OO programming is-a denotes inheritance and heirarchy. Has-a means the class contains other classes so you would expect a phone book class would have phone numbers and addresses.
koppe74
2009-09-16 09:53:56 UTC
1) Arrays are really pointers, so the index tells you the offset. The first element is located at the base-address itself, and thus has an offset of 0 (zero) -- which in array-notation is adress[0]. The name of an array (without the index) is the *address* of -- i.e. the *pointer* to -- the first element (array[0]). So arrays and pointers are interchangable.



array = &(array[0])



array[0] = *(array) = *(array+0) Offset of zero

array[1] = *(array+1) Offset of 1

array[2] = *(array+2) Offset of 2

array[3] = *(array+3) Offset of 3



2) I think you got that wrong... It's "char *argv[]" , or if you like (as arrays and pointers are the same thing) "char **argv"... not "char *argv".



Strings in C and C++ are really NULL-terminated char-arrays, so "char *" (char-pointer) tells you that this is an array of chars... and as it's used NULL-terminated, it is a string. (Again pointers and arrays are the same).



The reason for the array -- the "[]" at the end or the extra "*" -- is that you have an array of strings... in other words, an array of NULL-terminated char-arrays.



You have an array containing pointers... these pointers, each points to a string -- a NULL-terminated char-array/char-pointer. That means the whole thing will be a "table of char-pointers", "a table of char tables" or "a pointer to char-pointers".



Each of the pointers point to a string containing one argumet -- with the first element (argv[0]) pointing to the program-name itself. argv[1] points to a string with the 1st argument, argv[2] points to a string with the 2nd argument.



As your after particular arguments, it's better to do it this way, than having one big string containing the whole command-line.



3) No idea, sorry...
?
2009-09-16 09:46:47 UTC
1. Because 0-based indexing is the simplest and most natural way to address an array on a computer - you just multiply the index by the element size and you have an address offset into the array. It's only humans who prefer 1-based addressing.



2. Because argv is an array of strings (0..argc-1), not just a single string.



3. A dog is-a quadruped, but a dog has-a name. So a dog class might be a derived class of a quadruped class, but a dog class might also include a name class.
just "JR"
2009-09-16 09:37:17 UTC
1. The index of an array is an OFFSET from the beginning of the array, multiplied by the width of the array items:

Say an array of integers, 4 bytes long.

Say beginning of array at address 0x1000

First integer address: BaseAddress + (index * len) = 0x1000 + ( 0 * 4) = 0x1000

Second integer will be at : 0x1000 + ( 1 * 4 ) = 0x1004.

2. A pointer takes less "space" (an address) than a string, less stack used. It COULD be a string, but it is not "reasonnable".

3. never heard of that one! (despite 40 years of programming)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...