Dereferencing arrays that contain pointers in C -


I wrote a program that included two rows of integers which used to swap from time to time. Therefore, what I did was allocated to two different pointers that would display the required rows. I then placed both pointers in a static indicator array which would easily facilitate swapping.

The problem I have is by mistake, for example, take it:

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Int main (zero) {char * a = malloc (size of (four) * 5); Char * temp [] = {a}; Int i, h, k = 'a'; For (i = 0; i <5; i ++) a [i] = k ++; For (i = 0; i <5; i ++) printf ("% c", temporary [0] [i]); Free (a); Return 0; }   

with output: abcde

I accidentally use the members of the array contained within the static array by dereferencing the second order I tried: "[] []"

I did not feel much about this incident because everything was compiled and ran completely, even in the production of expected results for. The problem came when I ran a memory analysis and found that I was getting the addresses of many errors and alleged leaks.

Therefore, to start the debugging process I wanted to come here and ask why this works and if it is a valid syntax. I was under the impression that:

  temp [0]   

to char * , A . After this, it will take more dereference of a to use its members. Besides, I was under the impression:

  temp [row] [col ]   

has been translated into:

  * (* (* (float + row * call) + cola)   

The dual dereference of temp [0] [i] should be given an offset by temp and a at run time. What should be more interesting is that:

  #include  gt;  {main}}; {char * a = malloc (size of four) * 5); four * b = molok (size (four) * 5); four * temp [] = {A [i] = k ++; for i, h, k = 'a', j = 'a'; (i = 0; i & lt; 5; i ++) for {a, b}; int i, h, (I = 0; i & lt; 5; i ++) for {i] = J ++;} {for (h = 0; h <5; h ++) printf ("% c" , Floating [i] [h]); puts ("");} free (a); free (b); return 0;}   

when not compiled it happens. However, this code does compile it using Microsoft Visual Studio Professional 2013 and GCC in the c99 mode.

Any ideas or suggestions how to properly do what I was trying to do? That is, access the members to a and b , which are actually included in the constant array temp without using ugly syntax:

  * (temp [i] + sizeof (char) * h)    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -