Wednesday 15 July 2015

c - Is an int pointer an array of ints? -


I'm following a few examples which declare an integer pointer

  Int * myInt;   

and then turn that pointer into an array

  myInt = (int *) malloc (1024);   

Checks it

  myInt [0] = 5; Cout & lt; & Lt; Minute [0]; // print 5 myint [1] = 7; Cout & lt; & Lt; Mint [1]; // print 7   

I thought an integer indicator was an indicator for an int and never at all. I know that the signal to wire only indicates the first letter of the string, but it appears that there is something similar in the same kind of things here. But then what we want is an array of things, why not create an array of inset instead of an indicator rather than an int?

The way I work in C, C ++ is in a C ++ file but the related code is in C.

There is an optional syntax for accessing items indicated by a pointer - square brackets This syntax gives you points of Pointer Allows access to data by means such that an indicator was an array (of course, the indicators are not arrays) is a [i] an expression, which is an alternative form of * (a + i) * ).

When you allocate the dynamic storage and assign it to myInt , then you can use the pointer as a dynamic array that can resize at runtime:

  myInt = malloc (1024 * sizeof (int)); // C does not need an artist, only for C ++ (int i = 0; i! = 1024; i ++) {myInt [i] = i; // square bracket syntax for use} (int i = 0; i! = 1024; i ++) {printf ("% d", * (myInt + i)); // Use the equivalent indicator syntax)   


* Incidentally, write the comma of + Returns 4 [array] instead of array [4] ; Do not do this!

No comments:

Post a Comment