#include <stdio.h>
int main()
{
int a [2][3][2]={{{1,2},{3,4},{5,6}},{{5,8},{9,10},{11,12}}};
printf("%d\n%d\n%d\n",a[1]-a[0],a[1][0]-a[0][0],a[1][0][0]-a[0][0][0]);
return 0;
}
The output is 3 6 4. Can anyone explain to me the reason for this? How come a[1]-a[0]=3 and a[1][0]-a[0][0]=6 and how a[] and a[][] interprets in a 3-dimensional array?
It might help if you understand how an array like yours is laid out in memory:
+------------+ Low address +---------+ Low address +------+
| a[0][0][0] | | a[0][0] | | a[0] |
| a[0][0][1] | | | | |
| a[0][1][0] | | a[0][1] | | |
| a[0][1][1] | | | | |
| a[0][2][0] | | a[0][2] | | |
| a[0][2][1] | | | | |
| a[1][0][0] | | a[1][0] | | a[1] |
| a[1][0][1] | | | | |
| a[1][1][0] | | a[1][1] | | |
| a[1][1][1] | | | | |
| a[1][2][0] | | a[1][2] | | |
| a[1][2][1] | | | | |
+------------+ High address +---------+ High address +------+
Then it helps to know that the difference you get is in multiples of the type. So for a[0] and a[1] the type is int[3][2] and there are three of those multiples between a[0] and a[1].
Same for a[0][0] and a[1][0], the type is int[2], and the difference is six int[2] units between a[0][0] and a[1][0].
To elaborate a little: Between a[0] and a[1] you have a[0][0], a[0][1] and a[0][2]. Three entries.
Between a[0][0] and a[1][0] you have a[0][0][0], a[0][0][1], a[0][1][0], a[0][1][1], a[0][2][0] anda[0][2][1]. Six entries.
At the point of address, a[1] and a[1][0] are the same value. And a[0] and a[0][0] are same value.
But the types are different.
a[1][0] and a[0][0] are int *, from a[0][0] to a[1][0], there are 6 int.
And from a[1] to a[0], there are 3 {x, y}.
a[1][0][0] and a[0][0][0] are int, a[1][0][0]-a[0][0][0] = 5 - 1 = 4.
In C, a multi-dimensional array is conceptually an array whose elements are also arrays. So if you do:
int array[2][3]; Conceptually you end up with:
array[0] => [0, 1, 2]
array[1] => [0, 1, 2]
int array[2][3][2]; ...will give you a structure like:
array[0] => [0] => [1, 2]
[1] => [3, 4]
[2] => [5, 6]
array[1] => [0] => [5, 8]
[1] => [9, 10]
[2] => [11, 12]
a[1]-a[0] => will give difference you get is type of unit. a[0] and a[1] is int and there are three unit between them.similarly for the second part
a[1][0]-a[0][0]=6
number of combination for between a[0][0] and a[1][0] is 6.
Related
Could you explain how the output is -4? I think ++pp; is UB but not sure. Your explanation will really help in the way of understanding. Could be there any difference of outputs in big-endian or little-endian machine?
#include <stdio.h>
int a[] = { -1, -2, -3, -4 };
int b[] = { 0, 1, 2, 3 };
int main(void)
{
int *p[] = { a, b };
int **pp = p;
printf("a=%p, b=%p, p=%p, pp=%p\n", (void*)a, (void*)b, (void*)p, (void*)pp);
++pp;
printf("p=%p, pp=%p *pp=%p\n", (void*)p, (void*)pp, (void*)*pp);
++*pp;
printf("p=%p, pp=%p *pp=%p\n", (void*)p, (void*)pp, (void*)*pp);
++**pp;
printf("%d\n", (++**pp)[a]);
}
My output:
a=0x107121040, b=0x107121050, p=0x7ffee8adfad0, pp=0x7ffee8adfad0
p=0x7ffee8adfad0, pp=0x7ffee8adfad8 *pp=0x107121050
p=0x7ffee8adfad0, pp=0x7ffee8adfad8 *pp=0x107121054
-4
Ideone output
When you use the name of an array (in most contexts), it decays to a pointer to its first element. That means that int* p = a; and int* p = &a[0]; are exactly the same.
So to understand what happens in this case, just walk through step by step. At the point of your first printf call, things look like this:
pp p a
+-------+ +------+ +----+----+----+----+
| +---------> +--------> -1 | -2 | -3 | -4 |
+-------+ | | +----+----+----+----+
| |
+------+ b
| | +----+----+----+----+
| +---------> 0 | 1 | 2 | 3 |
| | +----+----+----+----+
+------+
pp points to the first element of p, which is a pointer to the first element of a.
Now, when you increment pp, it changes to point to the second element of p, which is a pointer to the first element of b:
pp p a
+-------+ +------+ +----+----+----+----+
| + | | +--------> -1 | -2 | -3 | -4 |
+---|---+ | | +----+----+----+----+
| | |
| +------+ b
| | | +----+----+----+----+
+---------> +---------> 0 | 1 | 2 | 3 |
| | +----+----+----+----+
+------+
You then increment *pp. Since *pp is a pointer to the first element of b, that pointer is incremented to point to the second element of b:
pp p a
+-------+ +------+ +----+----+----+----+
| + | | +--------> -1 | -2 | -3 | -4 |
+---|---+ | | +----+----+----+----+
| | |
| +------+ b
| | | +----+----+----+----+
+---------> | | 0 | 1 | 2 | 3 |
| + | +----+-^--+----+----+
+---|--+ |
+---------------+
Then you increment **pp. At this point pp is a pointer to the second element of p, so *pp is a pointer to the second element of b. That means **pp names the second element of b. You increment that from 1 to 2:
pp p a
+-------+ +------+ +----+----+----+----+
| + | | +--------> -1 | -2 | -3 | -4 |
+---|---+ | | +----+----+----+----+
| | |
| +------+ b
| | | +----+----+----+----+
+---------> | | 0 | 2 | 2 | 3 |
| + | +----+-^--+----+----+
+---|--+ |
+---------------+
Now, lets dissect (++**pp)[a]. ++**pp is the same as before, so the second element of b gets incremented to 3.
Now, for any pointer ptr and integer n, ptr[n] is the same as *(ptr + n). Since addition is commutative, ptr + n is the same as n + ptr. That means ptr[n] is the same as n[ptr].
Putting these together, that means that (++**pp)[a] is the same as 3[a], which is the same as a[3]. a[3] is -4, hence your result.
Remember the definition of the subscription operator [], e.g. as defined in this online C standard draft:
6.5.2.1 Array subscripting
2) ... The definition of the subscript operator [] is that E1[E2] is
identical to (*((E1)+(E2))). ...
It says that E1[E2] is identical to (*((E1)+(E2)).Then it becomes clear that (++**pp)[a] is the same as *((++**pp)+(a)), which again is the same as *((a)+(++**pp)), which consequently reads as a[(++**pp)]. The value of ++**pp is 3 then, and a[3] is -4.
It's easiest to understand this if you express all the array names in expressions as their decayed values. arrayName as a pointer becomes &arrayName[0]. So after all the initializations, you have:
a[0] = -1, a[1] = -2, a[2] = -3, a[3] = -4
b[0] = 0, b[1] = 1, b[2] = 2, b[3] = 3
p[0] = &a[0], p[1] = &b[0]
pp = &p[0]
Incrementing a pointer makes it point to the next array element, so after ++pp we now have
pp = &p[1]
++*pp dereferences pp, so it's equivalent to ++p[1], so now we have
p[1] = &b[1]
++**pp dereferences this twice, so it's equivalent to ++b[1], so now we have
b[1] = 2
Finally, we have the really confusing expression (++**pp)[a]. ++**pp again increments b[1], so its value is now 3, and that value replaces that expression, so it's equivalent to 3[a]. This might look like nonsense (3 isn't an array, how can you index it?), but it turns out that in C, x[y] == y[x] because of the way indexing is defined in terms of pointer arithmetic. So 3[a] is the same as a[3], and the last line prints -4.
I want to realloc 2d pointer array. It must be dynamic as follows ascii
+=====+==============+==============+==============+==============+======+
| | [0] | [1] | [2] | [3] | [..] |
+=====+==============+==============+==============+==============+======+
| [0] | 'a' | 'b' | 'c' | 'd' | |
+-----+--------------+--------------+--------------+--------------+------+
| [1] | object[0][0] | object[1][0] | object[2][0] | object[3][0] | |
+-----+--------------+--------------+--------------+--------------+------+
| [2] | object[0][1] | object[1][1] | object[2][1] | object[3][1] | |
+-----+--------------+--------------+--------------+--------------+------+
| [3] | object[0][2] | object[1][2] | object[2][2] | | |
+-----+--------------+--------------+--------------+--------------+------+
| [4] | object[0][3] | | object[2][3] | | |
+-----+--------------+--------------+--------------+--------------+------+
| [5] | object[0][4] | | | | |
+-----+--------------+--------------+--------------+--------------+------+
| [6] | object[0][5] | | | | |
+-----+--------------+--------------+--------------+--------------+------+
In this table every cols size is different. How can i do this with 2d struct. I allocated matrix with malloc. But i want to realloc second index. Like this matrix[25][n]. N must be realloc for every column with different size. But it must be on runtime
Code :
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char word[20];
}Dictionary;
Dictionary **object;
void initializeDictionary()
{
// Create matrix [29][1]
object=(Dictionary**)malloc(29 * sizeof(Dictionary*));
object[0]=(Dictionary*)malloc(1*sizeof(Dictionary));
}
With pointers this comes naturally. In your code you have a Dictionary** which indeed is a pointer to pointer to Dictionary. But you can see it an array of Dictionary* in which each pointer points to a different sized array of Dictionary objects.
Dictionary** objects;
const int COLUMNS = 29;
objects = malloc(COLUMNS * sizeof(Dictionary*));
objects[0] = malloc(2 * sizeof(Dictionary)); // first column 2 elements
objects[1] = malloc(3 * sizeof(Dictionary)); // second column 3 elements
// ...
for (int i = 0; i < COLUMNS; ++i)
free(objects[i]);
free(objects);
When we're defining a 2D array as in:
int *a[5];
Which dimension does the "5" define? The first or the second?
It's not a "2D" array. It's a 1-dimensional array of pointers to int. As such the array size designates that it has space for 5 pointers. Each individual pointer can point to the first element of a buffer with different size.
A "true 2D array" is the colloquial "array of arrays" int a[M][N]. Here the expression a[i] evaluates to the array of N integers, at position i.
Each a[i] points to a single int, which may be the first element in a sequence of int objects, like so:
a[0] a[1] a[2] a[3] a[4]
+----+----+----+----+----+
| | | | | |
+----+----+----+----+----+
| | | | |
| | | ... ...
| | +-------------------------------+
| +-------------------+ |
+-------+ | |
| | |
v v v
+---+ +---+ +---+
a[0][0] | | a[1][0] | | a[2][0] | |
+---+ +---+ +---+
a[0][1] | | a[1][1] | | a[2][1] | |
+---+ +---+ +---+
... ... ...
Thus, each a[i] can represent a "row" in your structure. You can dynamically allocate each "row" as
a[i] = malloc( sizeof *a[i] * row_length_for_i );
or you can set it to point to an existing array:
int foo[] = { 1, 2, 3 };
int bar[] = { 5, 6, 7, 8, 9 };
...
a[0] = foo;
a[1] = bar;
As shown in the example above, each "row" may have a different length.
I keep putting scare quotes around "row" because what you have is not a true 2D array - it's not a contiguous sequence of elements. The object immediately following a[0][N-1] will most likely not be a[1][0]. What you have is a sequence of pointers, each of which may point to the first element of a sequence of int, or to a single int, or to nothing at all.
I want to create an integer array[5][10] using malloc(). The difference between memory address of array[0] and array[1] is showing 8. Why?
#include <stdio.h>
#include <stdlib.h>
int main() {
int *b[5];
for (int loop = 0; loop < 5; loop++)
b[loop] = (int*)malloc(10 * sizeof(int));
printf("b=%u \n", b);
printf("(b+1)=%u \n", (b + 1));
printf("(b+2)=%u \n", (b + 2));
}
The output is:
b=2151122304
(b+1)=2151122312
(b+2)=2151122320
The difference between memory address of array[0] and array[1] is showing 8. Why?
That's because sizeof of a pointer on your platform is 8.
BTW, use of %u to print a pointer leads to undefined behavior. Use %p instead.
printf("(b+1)=%p \n",(b+1));
printf("(b+2)=%p \n",(b+2));
Difference between array of pointers and a 2D array
When you use:
int *b[5];
The memory used for b is:
&b[0] &b[1] &b[2]
| | |
v v v
+--------+--------+--------+
| b[0] | b[1] | b[2] |
+--------+--------+--------+
(b+1) is the same as &b[1]
(b+2) is the same as &b[2]
Hence, the difference between (b+2) and (b+1) is the size of a pointer.
When you use:
int b[5][10];
The memory used for b is:
&b[0][0] &b[1][0] &b[2][0]
| | |
v v v
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ ...
| | | | | | | | | | | | | | | | | | | | | ...
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ ...
(b+1) is the same as &b[1], The value of that pointer is the same as the value of &b[1][0] even though they are pointers to different types.
(b+2) is the same as &b[2], The value of that pointer is the same as the value of &b[2][0]
Hence, the difference between (b+2) and (b+1) is the size of 10 ints.
First, with int *b[5] you are not creating a two dimensional array, but an array of pointers.
The elements of the array b are pointers. Each occupies the size of a pointer, which depends on your architecture. In a 64-bits architecture it will probably occupy 64 bits (8 bytes). You can check that by printing sizeof(int*) or sizeof(b[0])
Memory allocation will look like
b
+-----+
| | +------+------+-----------+-----+-----+-----+-----+
| b[0]+--------------> | | | | | | | |
| | +------+------+-----------+-----+-----+-----+-----+
+-----+
| | +------+------+-----------+-----+-----+-----+-----+
| b[1]+--------------> | | |....... | | | | |
| | +------+------+-----------+-----+-----+-----+-----+
+-----+
| | +------+------+-----------+-----+-----+-----+-----+
| b[2]+--------------> | | | ...... | | | | |
| | +------+------+-----------+-----+-----+-----+-----+
+-----+
| | +------+------+-----------+-----+-----+-----+-----+
| b[3]+--------------> | | | ...... | | | | |
| | +------+------+-----------+-----+-----+-----+-----+
+-----+
| | +------+------+-----------+-----+-----+-----+-----+
| b[4]+--------------> | | | ...... | | | | |
| | +------+------+-----------+-----+-----+-----+-----+
+-----+
b will point to b[0], after decay, and b + 1 will give the address of b[1]. Size of pointer on your machine is 8 bytes, therefore you are getting a difference of 8 in the address.
Beside of this
Do not cast return value of malloc
b[loop]=malloc(10*sizeof(int));
and use %p for pointer data type
printf("b=%p \n",(void *)b);
printf("(b+1)=%p \n",(void *)(b+1));
printf("(b+2)=%p \n",(void *)(b+2));
What you've declared is not technically a two dimensional array but an array of pointers to int, each of which points to an array of int. The reason array[0] and array[1] are 8 bytes apart is because you have an array of pointers, and pointers on your system are 8 bytes.
When you allocate each individual 1 dimensional array, they don't necessarily exist next to each other in memory. If on the other hand you declared int b[5][10], you would have 10 * 5 = 50 contiguous integers arranged in 5 rows of 10.
The program below has unexpected output.
func main(){
s:=[]int{5}
s=append(s,7)
s=append(s,9)
x:=append(s,11)
y:=append(s,12)
fmt.Println(s,x,y)
}
output: [5 7 9] [5 7 9 12] [5 7 9 12]
Why is the last element of x 12?
A slice is only a window over part of an array, it has no specific storage.
This means that if you have two slices over the same part of an array, both slices must "contain" the same values.
Here's exactly what happens here :
When you do the first append, you get a new slice of size 2 over an underlying array of size 2.
When you do the next append, you get a new slice of size 3 but the underlying array is of size 4 (append usually allocates more space than the immediately needed one so that it doesn't need to allocate at every append).
This means the next append doesn't need a new array. So x and y both will use the same underlying array as the precedent slice s. You write 11 and then 12 in the same slot of this array, even if you get two different slices (remember, they're just windows).
You can check that by printing the capacity of the slice after each append :
fmt.Println(cap(s))
If you want to have different values in x and y, you should do a copy, for example like this :
s := []int{5}
s = append(s, 7)
s = append(s, 9)
x := make([]int,len(s))
copy(x,s)
x = append(x, 11)
y := append(s, 12)
fmt.Println(s, x, y)
Another solution here might have been to force the capacity of the array behind the s slice to be not greater than the needed one (thus ensuring the two following append have to use a new array) :
s := []int{5}
s = append(s, 7)
s = append(s, 9)
s = s[0:len(s):len(s)]
x := append(s, 11)
y := append(s, 12)
fmt.Println(s, x, y)
See also Re-slicing slices in Golang
dystroy explained it very well. I like to add a visual explanation to the behaviour.
A slice is only a descriptor of an array segment. It consists of a pointer to the array (ptr), the length of the segment (len), and capacity (cap).
+-----+
| ptr |
|*Elem|
+-----+
| len |
|int |
+-----+
| cap |
|int |
+-----+
So, the explanation of the code is as follow;
func main() {
+
|
s := []int{5} | s -> +-----+
| []int | ptr +-----> +---+
| |*int | [1]int| 5 |
| +-----+ +---+
| |len=1|
| |int |
| +-----+
| |cap=1|
| |int |
| +-----+
|
s = append(s,7) | s -> +-----+
| []int | ptr +-----> +---+---+
| |*int | [2]int| 5 | 7 |
| +-----+ +---+---+
| |len=2|
| |int |
| +-----+
| |cap=2|
| |int |
| +-----+
|
s = append(s,9) | s -> +-----+
| []int | ptr +-----> +---+---+---+---+
| |*int | [4]int| 5 | 7 | 9 | |
| +-----+ +---+---+---+---+
| |len=3|
| |int |
| +-----+
| |cap=4|
| |int |
| +-----+
|
x := append(s,11) | +-------------+-----> +---+---+---+---+
| | | [4]int| 5 | 7 | 9 |11 |
| | | +---+---+---+---+
| s -> +--+--+ x -> +--+--+
| []int | ptr | []int | ptr |
| |*int | |*int |
| +-----+ +-----+
| |len=3| |len=4|
| |int | |int |
| +-----+ +-----+
| |cap=4| |cap=4|
| |int | |int |
| +-----+ +-----+
|
y := append(s,12) | +-----> +---+---+---+---+
| | [4]int| 5 | 7 | 9 |12 |
| | +---+---+---+---+
| |
| +-------------+-------------+
| | | |
| s -> +--+--+ x -> +--+--+ y -> +--+--+
| []int | ptr | []int | ptr | []int | ptr |
| |*int | |*int | |*int |
| +-----+ +-----+ +-----+
| |len=3| |len=4| |len=4|
| |int | |int | |int |
| +-----+ +-----+ +-----+
| |cap=4| |cap=4| |cap=4|
| |int | |int | |int |
+ +-----+ +-----+ +-----+
fmt.Println(s,x,y)
}