Thursday, July 9, 2009

How do you declare a pointer in c and what are 2 dimensional?

2 dimensional means having an array with two dimensions, have a look a this x=[4] means that the array contains 5 data containers lets say x[4]={0,1,2,3,4} while 2 dimensional is like this x[4][5] which by doing the math would return you 20 array element values, why? because it can be easily be said as 4x5 each column has 5 rows, got it? hope so, now for pointers, it can be this way, but beware i take no account for any damage you could inflict your system specially you memory stick.





pointers are as the name suggest, points directly to the address block where the data is reference unlike a variable such x=5 it tells the compiler to put five into x where as


*x;


y=5;


x=y


generally puts 5 into where x is actually at.





you start pointer by using the * or what we call the reference pointer





here for your example:





pSamplVar = %26amp;samplVar;


*pSamplVar =12;





the first line means make pSamplVar equal to the address of samplVar and the second line means make the contents of address pSamplVar equal to 12, so this makes samplVar equal to 12


you can also try to characters;





i have made you a simple program here:





assuming you use c





#include %26lt;stdio.h%26gt;


main()


{


int num1, num2, total;


num1=5;


num2=3;


total = intadd(%26amp;num1, %26amp;num2);


printf("%d plus %d equals to %d", num1, num2, total);


}





intadd (x,y)


int *x,*y;


{


*x=*x+1;


*y=*y+1;


return *x + *y


}





the result is 10





good luck!!!


No comments:

Post a Comment