Você está na página 1de 18

C Programming Reference >> C Basic Programming Tutorials >>

Pointersquisite Knowledege
21/07/07 - 12746 Views - Ratings :

4.29 of 5 / 87 Votes

Level - Beginner

Pointer is the feature which has grabbed most attention in C Programming combined all other
features . Pointers are most powerfull feature but can be also most dangerous if abused. The
pointers in C are used in my task which cannot be achieved by any other mean, such as dynamic
memmory allocation, changing value using function, dynamic data structures etc. . They are also
much much efficient in use of system resources than thier counter part and can be used to
enhance your program speed and reduce its size. The success of a c programmer depends
crucially on his understanding of C Pointers and that us reason why we are going to discuss them
here in painstaking detail.
Contents 1. Pointer Fundamenta
2. Pointer Variable
3. Pointer Assignment
4. Pointer Conversion
5. Pointer Arithemetic
6. Pointer Comparison
7. Arrays And Pointers
8. Multiple Indirection
9. Pointer Intialization
10. Pointer To A Function
11. Dynamic Memmory Allocation Using Pointers
12. Restrict Pointers
13. Pointer Drawbacks

1. Pointers Fundamentals
In the simplest term pointer is a nearly integer variable which stores a memmory address of a
computer which may contain other variable or even another pointer.
If a variable contains address of another variable than it is said that first variable points to
second. Pointer can also be represented as a refrence to another variable but there is very subtle
diffrence in the two statements which is mostly dependent upon situation and enviorment.
Pointer is generally of size of integer on the machine but it may be of diffrent type which
indicates the type of variable the pointer is pointing to decides pointers properties and behaviour.
Pointer of one type cannot be implicitly converted from one type to another but can be explicitly
converted using type casting. In such a conversion a pointer always assumes that it is point to a
object of its type but reality may differ and if used incorrectly may lead to disasters including
permanent machine damage.

Also it is important to note that all operation perform on pointers are done through two operators
'*' (Star) and '&' (Ampercent). '&' is a unary operator that returns a memmory address of a
variable. '*' is complement of '&' and return value stored at a memmory location stored in a
pointer. '*' can interpreted as statement "at address" while '&' can be interpreted as statement
"address of".
Back To Top
2. Pointer Variable
Declaring a pointer variable is quite similar to declaring an normal variable all you have to do is
to insert a star '*' operator before it.
General form of pointer declaration is type* name;

where type represent the type to which pointer thinks it is pointing to.
Pointers to machine defined as well as user-defined types can be made.
Multiple pointers of similar type can be declared in one statement but make sure you use *
before every one otherwise they will become a variable of that type.
Back To Top

3. Pointer Assignment
This is a type of expression used to assign value of one pointer to another using assignment
operator '=' . In this value of right hand side points to memmory address of variable stored in left
hand side pointer. As a result both pointers point to same memmory location after this
expression.
Pointer of similar type can be used in expression easily as shown below but for diffrent type
pointers you need to type cast them as shown in next section.
#include <stdio.h>
int main ()
{
char ch = a;
char* p1, *p2;
p1 = &ch;
p2 = p1; // Pointer Assignement Taking Place
printf (" *p1 = %c And *p2 = %c", *p1,*p2); // Prints 'a' twice

return 0;
}

Back To Top
4. Pointer Conversion
Pointer Conversion is a very powerfull yet very dangerous feature. Before concept of pointer
conversion you must understand the concept of a void pointer. Void pointer technically is a
pointer which is pointing to the unknown. Void pointer has special property that it can be type
casted into anyother pointer without any type casting though every other conversion needs an
type casting. Also note that in C++ even void pointer needs an type casting so to maintain the
compatibility you may want to type cast void pointer anyway. Also in dynamic memmory
allocation function such as malloc ( ) and alloc ( ) returns void pointer which can be easily
converted to other types.
Also there is a pointer called null pointer which seems like void pointer but is entirely diffrent.
Null pointer is a pointer which points to nothing. Infact it points to the base address of you CPU
register and since register is not addressable usage of a null pointer will lead to crash or at
minimum a segmentation fault.
Also be careful while typecasting one pointer to another because even after type casting your
pointer can point to anything but it will still think it is pointing to something of it declared type
and have properties of the orignal type.
Type conversion is a powerfull feature but yet it may lead difficult to remove bugs and crashes
and should be used with uttermost vigilance. It may also lead to unexpected and unreliable
results but program would compile succesfully.
Code below shows a type casting of one pointer into another #include <stdio.h>
int main ()
{
int i = 10;
char* p1
int *p2;
p2 = &i;
p1 = (char *) p2; // Type Casting and Pointer Conversion
printf (" *p1 = %c And *p2 = %d", *p1,*p2); // Output maybe unexpected depending critic
return 0;
}

Back To Top
5. Pointer Arithmetic
Pointer arithemetic is quite diffrent from normal arithemetic unless and until you are work on
char type pointers, reason being they are 1 byte (special thnks to Emmanuel Deloget for pointing
mistake) long under all enviorments.
Not all artihemetic operations are defined in pointers. You can increment them, decrement them,
add and subtract integer values from them. You even can subtract two pointers.But you cannot
add two pointers, mulitply, divide,modulus them. You can not also add or subtract values other
than integer.
Now pointer artihemetic may look a little wierd but it has a deep sense attach to it.
Now consider a pointer X , its current value that address it is pointing to is 1000 (just
assuming).We make another assumption about the size of the data types. Size of data type is
machine dependent, for example int can be 16,32, or 64 bit long depending upon your machine.
Now if this X pointer is char type(assumed 1 Byte or 8Bit long) than X++ will have value 1001
and X-- will have value 999. Now if this X pointer is integer type (assumed 2 byte or 32 bit long)
than X++ will have value 1002 and X-- will have value 998. Again if this X pointer is float type
(assumed 4 Byte or 32Bit long) than X++ will have value 1004 and X-- will have value 9996.
Also if this X pointer is double type(assumed 8 Byte or 64 Bit long) than X++ will have value
1008 and X-- will have value 992. Do you see the pattern here.
Reason is when you increment a pointer of certain base type it increase it value in such a way
that it points to next element of its base type. If you decrement a pointer its value decrease in
such a way that it points to previous value of its base type. So increment as well as decrement in
fixed quanta of size of the base type.
You can add or subtract any integer value, in such case value of pointer get increase and decrease
by the productv of the value to be added or subtract and size of the base type.
Pointer of user defined types such as structures and union also increase by the quanta of thier bit
values which can be determined using sizeof operator.
Pointer arithemtic in C may look a bit strange but it is extensively used in programming and
provides unmatched efficiency in performance of task such as accessing an array.
Back To Top

6. Pointer Comparison

Two pointers can be compared no matter where they point. Comparison can be done using <, >,
=, <= and >= operators. Though it is not forcibly implied but comparison of two pointers become
sensible only when they are related such as when they are poiniting to element of same
arrays.Comparison of two unrelated pointers is unpredictable and your code should not rely upon
it. All comparison are generally done on basis of memmory organization in the host machine.
Following C source code shows a potential use of pointer comparison in C #include <stdio.h>
int main ()
{
int data[100];
int* p1;
int *p2;
for (int i = 0; i <100;i++)
{
data[i] = i;
}
p1 = &data [1];
p2 = &data [2];
if (p1 > p2)
{
printf ("\n\n p1 is greater than p2");
}
else
{
printf ("\n\n p2 is greater than p1");
}
}

Back To Top
7. Array and Pointers
Array and Pointers in c are very closely related. Infact they are so similar to each othere in nature
that they can be used interchangebly in each other positions most of the time.
Important link joining them is that array name without the brackets is the pointer name and other
end a pointer can be indexed as if its an array. Consider following C Source Code for better
understanding in which cycle through the elements of array using a pointer as a array name and
through pointer arithemetic.
#include <stdio.h>
int main ()
{
int data[100];

int* p1;
for (int i = 0; i <100;i++)
{
data[i] = i;
}
p1 = data; //Assigning base address of an array to pointer
for (int i = 0; i <100;i++) //Accessing Array using index
{
printf ("\n%d",p1[i]);
}
for (int i = 0; i <100;i++) //Access Array using Pointer Arithemetic
{
printf ("\n%d",*(p1 +i));
}

return 0;
}

Pointers like any other data type can be arrayed. This array is called array of pointers and is quite
usefull programming method. Array of pointers are declared as shown below data_type *variable_name [array_size];

For eg to declare an array of 5 int pointers we will use declaration int *array [5];

The C Source code below will show you a simple use of array of pointers in c programming.
#include <stdio.h>
int main ()
{
int data[5];
int *array[5];
for (int i = 0; i <5;i++)
{
data[i] = i;
}
//Assigning address of elements of array data to array of pointers.
for (int i = 0; i <5;i++)

{
array[i] = &data[i];
}
for (int i = 0; i <5;i++) //Accessing Array value using index
{
printf ("\n%d",data[i]);
}
for (int i = 0; i <5;i++) //Access Array value using array of pointers
{
printf ("\n%d",*array[i] );
}

return 0;
}

Back To Top
8. Multiple Indirection
Well, in C it is permitted for a pointer to point to another pointer. As a result many layers of
pointer can be formed and this called multiple indirection. As such there is no limitation on the
level of indirection but including great depth of indirection is difficult to follow and may lead to
errors. Normally more than 2 level depth indirection is not required.
A pointer to a pointer has declaration is similar to that of a normal pointer but have more asterix
sign before them indicating the depth of the pointer.
int ** pointer

The above statement creates a pointer which points to pointer to a variable with int value.
Following c source code will help you clear any doubts on multiple indirection.
#include <stdio.h>
int main ()
{
int i = 10;
int **p1
int *p2;
p2 = &i;
p1 = &p2; // Multiple indirection
printf (" **p1 = %d And *p2 = %d", **p1,*p2); //Statement will show 10 twice.
return 0;
}

Back To Top
9. Pointer Intialization
When it comes to pointer intialization, c programming standard goes nut. Standard does not
forces any intialization to a non static local pointer but it does state that global pointers and static
pointers get automatically initalize to null. So unintialized non static local pointer contains an
unknown value. If you try to use this pointer without giving it proper value you may crash your
program and even operating system in extreme cases and permanent damage to computer in term
of software and hardware can occur. So pointers are very nasty if used incorrectly.
Most important term in pointer intialization would be "null". It is a standard practice of top
qualilty c programmers that any pointer which is not pointing to valid memmory location should
be intialized to null showing that it is pointing to nothing and should not be used. In fact a null
pointer is pointing to something, it is pointing to the base address of your CPU register. Now
since register of CPU cannot be addressed the use of such pointer leads to abormal termination of
the program with a probable segmentation fault at runtime but no serious damage will be done to
your machine.A MACRO name NULL is also defined in many standard headers such as
<stdio.h>.
You can initialize a pointer to null using 2 methods as shown below variable_type *pointer_name = 0;

or
variable_type *pointer_name = NULL;

Even though null pointer should not be used directly, it does have some indirect uses such as,
they can be used in a comparison with other pointer. They mark an end of something such as
thier extensive use in data structures.
Also char pointers which are capable of saving strings or char array can be intialized in more
spectacular way as shown below char *pointer_name = "string value here";

In such a statement you may wonder since p is not an array and no memmory is allocated to it so
where does the string is actually stored. Answer to this question can be found in the technique
used by C Compilers to handle string constants. To handle such constants the compiler creates a
string table in which dynamic memmory is allocated to pointer from heap and string is stored in
this string table.
Back To Top
10. Pointer To A Function

One of the rarest used yet powerfull feature of C programming language is the pointer to a
function. As most of the people dont know that every function defined in c programming
language have a base address attached to it. This base address acts as an entering point into that
function. As you can now guess this address can be stored in a specially designed pointer known
as function pointer. Address of the function can be obtained by using pointer name without
parenthesis.
Once stored this pointer can be of tremendous use such that it can be used to calll that function
but most important feast it let you attain is that it allows you to pass function as arguments to
other functions as if it was a variable.
The most tricky thing about pointer to a function is declaration of the pointer that holds the base
address of the function, rest is piece of cake.The general form of declaration of a pointer to a
function is return_type (* pointer_name) ( variable1_type variable1_name
variable2_type variable2_name,....);

The declaration above tells compiler that pointer of name pointer_name will store a address of
function which return a value of return_type type. Also it will take variables of name
variable1_name, variable2_name of type variable1_type, variable2_type respectively and so on.
The simple c source code below will further clear your concept of pointer to a function in c
programming.
#include <stdio.h>
int add (int a, int b, int c)
{
return a+b+c;
}
int main ()
{
int (*function_pointer) (int,int,int); //Declaration of function pointer for storage of
function_pointer = add;
printf ("%d", function_pointer (1,2,3)); //Print 6 as output
return 0;
}

Back To Top
11. Dynamic MemmoryAllocation Using Pointers
Dynamic memmory allocation is one of the most powerfull feature of the c programming
language because it is the tool which lets you design dynamic softwares using c. Dynamic
software is something which changes itself on runtime according to situations created during
executions but static program in all circumstances follows the same path of execution. Through

DMA (Dynamic Memmory Allocation) you can obtain memmory at runtime. As you will be
knowing global variables are allocated storage at compile time from heap while local variables
are also allocated memmory from stack by the compiler, but in sum situations especailly true for
dynamic programs that memmory requirement cannot be known at compile time but depends
upon the input user gives interaction or some other dynamic values which keeps changing. In
such cases memmory requirement of the program may expand or shrink at run time and in this
DMA comes handy. DMA is also extensively used in data structures such as link list, trees etc
because their innate dynamic nature.
Memmory allocated by DMA is from heap. Heap is a free memmory available to your program
and can be safely worked on since it is not used by any other program, O.S. etc. Heap is
generally very large but can be exhausted as it is finite. Heap can also so be a severe restraining
factor in special situations such as embedded systems and microprocessors, keeping in mind that
they have very limited memmory resources. So it is surely a good practice to return the
memmory back to heap when it has no longer used so that it can be reentered in the
circualtionfor reuse. Also size of the heap can be determined by some compiler dependant
functions but no function for this purpose is defined in C Standard.
Their are four functions defined in c standard for dynamic memmory allocation - calloc, free,
malloc and realloc. But in the heart of DMA there are only 2 of them malloc and free. Malloc
stands for memmory allocations and is used to allocate memmory from the heap while free is
used to return allocated memmory from malloc back to heap. Both these functions uses a
standard library header <stdlib.h>
Warning !!! - free ( ) function should be used to free memmory only allocated previously from
malloc, realloc or calloc. Freeing a random or undefined or compiler allocated memmory can
lead to severe damage to the O.S., Compiler and Computer Hardware Itself, in form of nasty
system crashes.
The prototype of malloc ( ) function is void *malloc (size_t number_of_bytes)

Important thing to nore is malloc return a void pointer which can be converted to any pointer
type as explained in previous points. Also size_t is a special type of unsigned integer defined in
<stdlib.h> capable of storing largest memmory size that can be allocated using DMA,
number_of_bytes is a value of type size_t generally a integer indicating the amount of memmory
to be allocated. Function malloc ( ) will be returning a null pointer if memmory allocation fails
and will return a pointer to first region of memmory allocated when succsefull. It is also
recommended you check the pointer returned for failure in allocation before using the returned
memmory for increasing stability of your program, generally programmers provide some error
handling code in case of failures. Also this returned pointer never needs a typecast in C since it is
a void pointer, it is a good practice to do one since it is required by C++ and will produce a error
if you used C++ compiler for compilation.

Another commonly used operator used with malloc is sizeof operator which is used to calculate
the value of number_of_bytes by determing the size of the compiler as well as user defined types
and variables.
The prototype of free ( ) function is void free (void *p)

Function free ( ) is opposite of malloc and is used to return memmory previously allocated by
other DMA functions. Also only memmory allocated using DMA should be free using free ()
otherwise you may corrupt your memmory allocation system at minimum.
C Source code shown below shows simple method of using dynamic memmory allocation
elegantly #include <stdio.h>
#include <stdlib.h>
int main ()
{
int *p;
p = (int *) malloc ( sizeof (int) ); //Dynamic Memmory Allocation
if (p == NULL) //Incase of memmory allocation failure execute the error handling code bloc
{
printf ("\nOut of Memmory");
exit (1);
}
*p = 100;
printf ("\n p = %d", *p); //Display 100 ofcourse.
return 0;
}

Also the normal arrays can be increased in power and flexibilty using Dynamic memmory
allocation to be converted into dynamic allocated arrays. These dynamic allocated arrays though
have a little bit of complication involved with them in usage, so read carefully the explanation
given below. Also thier declaration varies entirely.
For 1 - D array it is quite simple. Allocate ample memmory using DMA and then access array
either using pointer arithemetic or indexing as shown in C Source Code below #define SIZE 10 //Size of 1D Array

#include <stdio.h>
#include <stdlib.h>
int main ()
{
int *p;
p = (int *) malloc ( SIZE * sizeof (int) ); //Dynamic Memmory Allocation of 1D Array

if (p == NULL) //Incase of memmory allocation failure execute the error handling code bloc
{
printf ("\nOut of Memmory");
exit (1);
}
for (i = 0; i<SIZE; i++)
{
p [i] = i; // Loading the Array
}
for (i = 0; i<SIZE; i++)
{
printf ("\n%d", *(p + i)); // Displaying the Array
}
return 0;
}

In case of 2-D array allocation similar to that above can be used but in that case accessing the
array is not possible using indexing the pointer to which memmory being allocted reason being
that the left index is not defined for the pointer that is it is not possible for compiler to know how
many rows and columns in the 2D array. In such cases you have to design code to access values
using complicated pointer arithemetic.
On other hand you can declare a pointer in a special way that compiler is informed of structure of
the 2D array so that access to that array can be provide using indexing or simple pointer
arithemetic as shown in C Source code below #define SIZE1 10 //Number of Rows in 2D Array
#define SIZE2 10 //Number of Columns in 2D Array
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int (*p) [SIZE1]; //Observe the special pointer declaration to tell compiler 2D array has

p = (int (*) [SIZE1]) malloc ( SIZE1 * SIZE2 * sizeof (int) ); //Dynamic Memmory Allocatio
if (p == NULL) //Incase of memmory allocation failure execute the error handling code bloc
{
printf ("\nOut of Memmory");
exit (1);
}
for (j = 0; j<SIZE1; j++)
{
for (i = 0; i<SIZE2; i++)
{
p [j] [i] = j*SIZE1 +i; // Loading the Array
}
}
for (j = 0; j<SIZE1; j++)
{
for (i = 0; i<SIZE2; i++)
{
printf ("\n%d",p [j] [i]); // Displaying the Array
}
}
return 0;
}

The concept above can be extended to 3D array and other complex multidimensional arrays.
Back To Top
12. Restrict Pointers
This a very innovative new feature added by C99. This is a brand new type qualifier applicable
only to pointers. Restrict qualifiers tells the compiler that pointer is the only mean by which the
object can be accessed. Another pointer can access it only if its based on the first one. This
enables complier to produce highly efficient code by assuming that if two pointers are declared
as restrict then these two pointers doesnot overlap. The primary use in pointers passed in
function parameter and standard library makes an extensive use of this powerpacked feature of
C. One important thing to remember is under any circumstances restrict pointer doesnot change
the semantics of the program. Restrict pointers may sometime allow the complier to optimize
code.
Also as mentioned by Emmanuel Deloget below in the comments, in c standard the behaviour is
marked undefined when two restrict pointers overlap. The standard lef t it to compilers to
implement them. Some may produce compile time error, some may produce run time error and
most will give atleast a warning. In a rare case compiler can even ignore it leading too extremely
dangerous bugs and errors. You should read your C compilers documentation for futher
information about its strategy on restrict pointers.

Code below shows a potenial use of restrict pointer-

void function (restrict int *a, restrict int *b) \\ a and b pointers cannot overlap ie. po
{
int temp = *a;
*a = *b;
b = temp;
//a = b; //If comment is removed this line will result in a compile time error in most co
}

Back To Top
13. Pointer Drawbacks
Pointers have tremendous power but the power can swing both sides good and evil. Pointer if
used incorrectly leads to very difficult to unearth bugs which will most probably make you go
wild. Pointers are itself are not any trouble but the value they are storing can be. If it contains a
incorrect value it can lead to disasters of massive magnitude when used.
When you use this incorrect pointer to read a memmory location, you may be reading a incorrect
garbage value which if unluckly accepted by your program as assumed correct value nothing can
help you. Consider a scenario in banking in which any customers real account value is switched
with this garbage value, he can become a millionare or beggar in a second, or think that in a
rocket launching software you use this incorrect value as launching angle and crashing the billion
dollar masterpiece. These scenarios are just my imagination runnig wild but you cannot ignore
the fact that they are possibility.
Now when you use this incorrect pointer to write a memmory location you may be writting a
unknown memmory location. If you have a large memmory in the system maybe you are using a
unassigned memmory but if that memmory by any luck is a memmory used by O.S. or Hardware
and you are modifying it you maybe corrupting your Operating System software or damaging
your hardware and thier drivers. Also it is a possibility that you maybe using a memmory
location alerady in use by your software storing some essential data and you are unknowingly
modifying it. You maybe writing over your own code and data.
Such bugs and errors created by the pointers maynot show up immediately but come up later and
it is at that time difficult to predict that it was the pointer to blamed.
To help you avoid these pointers error, we will be looking at some types of pointers error which
are possible and learn how to avoid them by using good programming practices.
>> Unintialized Pointers
Carefully look at the c source code below #include <stdio.h>

int main ()
{
int a, *p;
a = 1;
*p = a;
return 0;
}

Now notice above that pointer p above is pointing to some unknown location. Maybe if you are
luckly your compiler or O.S. points it to some safe location but it is just an assumption. It can be
pointing to any damn memmory location in the system. Now the code above write value 1 into
the unknown memmory location pointed to by p. There is a large possiblility with large program
in small enviorment that p is pointing something vital which is now destroyed. To avoid this
error you must use pointer intialization so that pointer is pointing to nothing and compiler issue
an error (runtime or compile time) when you use that pointer incorrectly and some damage
contol can happen.
Correct and Safe Code would be #include <stdio.h>
int main ()
{
int a, *p = NULL;
a = 1;
*p = a; //This would generate a run time error always and a compile time error in smart c
return 0;
}

>> Incorrect Pointer Usage


Carefully look at the c source code below #include <stdio.h>
#include <alloc.h>
int main ()
{
int a, *p = NULL;
p = (int *) malloc (sizeof (int));
a = 1;
p = a;
prinf ("%d",*p); //This will print garbage value
return 0;
}

Seeing above code i guess you can easily point the error. Line p = a; is incorrect and should be p
= &a;. The point is this maybe a syntax error by the programmer or a logical error by a begineer
it can be disastrous. Compiler should give warning but no compiler would produce error, infact

even a warning is only a assumption and may not occur. The reason being compiler except this
because every point in memmory is represented in hexa decimal number which this decimal
number is implicitly converted. It is a rare but usefull thing in special circumstances to use
hardcode memmory values especially in case of hardware programming and system
programming where values of memmory address such as port address always remain constant for
the machine for which the program is being used. But this concept allows accedential error like
above which may prove dangerous. So stay cautious of such error while programming.
Correct and Safe Code would be #include <stdio.h>
#include <alloc.h>
int main ()
{
int a, *p = NULL;
p = (int *) malloc (sizeof (int));
a = 1;
p = &a;
prinf ("%d",*p);
return 0;
}

>> Incorrect Pointer Comparison


Carefully look at the c source code below #include <stdio.h>
#include <alloc.h>
int main ()
{
int *p = NULL, *q = NULL;
p = (int *) malloc (sizeof (int));
q = (int *) malloc (sizeof (int));
if ( p < q )
prinf ("\np is less than q"); //This will print garbage value
else
printf ("\np is greater than q");
return 0;
}

The comparison of pointer as done above is absolutely incorrect reason being that both p and q
pointers in code above are entirely unrelated and the ouput of the above code cannot be
predicted. Comparison done above is entirely dependent upon compiler, operating system and to
great extent luck. When pointer comparison is performed, the comparison is done between the
integer values of the memmory locations they are pointing to. Now this memmory location
assigned to them is not fixed and is entirely random so p can be either less or greater than q each
with 50% probability in mathematics terms. So the comparison cannot be relied upon is the
bottom line. But this comparison if used correctly can be usefull in logically related memmory

allocation such as in arrays. Also in very very very rare conditions you may even want to use this
unrelated pointer comparison.
>> Failure to do pointer reintialization
Carefully look at the c source code below #include <stdio.h>
#include <alloc.h>
int main ()
{
int i;
int *p = (int *) malloc ( sizeof (int) * 10 );
int *q;
q = p;
for ( i = 0; i < 10; i++)
{
*p = i;
p++;
}
for ( i = 0; i < 10; i++)
{
printf (" %d ", *p); //Prints Garbage Value
*p++;
}
return 0;
}

The mistake above is very common and creates a bug very tough to found. The point is when in
above code p is allocated memmory and used to load int values using pointer arithemetic and
then when same pointer is used to display the loaded values output will be garbage value. The
reason for this that p is not reinitalized to memmory address it was given during dynamic
memmory allocation. So p keeps running ahead of its last left poisition that is from the end of its
allocated memmory and runs into unknown memmory locations next in serial and only god
knows what it will read and destroy when used for writting memmory location. This problem can
be easily avoided by reinitializing p to its original value.
Correct and Safe Code would be #include <stdio.h>
#include <alloc.h>
int main ()
{
int i;
int *p = (int *) malloc ( sizeof (int) * 10 );
int *q;

q = p;
for ( i = 0; i < 10; i++)
{
*p = i;
p++;
}
p = q;
for ( i = 0; i < 10; i++)
{
printf (" %d ", *p); //Prints Value 1 to 10;
*p++;
}
return 0;
}

Você também pode gostar