The site moved to root domain where all post are imported. Please go to http://pusheax.com/
This post is part of http://www.pusheax.com/2013/03/struct-and-typedef-of-c-programming.html . Such as struct, pointer, typedef example. I have did some more advanced things with struct:
//Yes We can declar a whole sentence in variable(Remember *):
char *str="We need to know programming for being a security resarcher.";
int *ptr; //We declaring c pointer which start with asteric!
int anotherN; // This int variable
anotherN=1337; //The current value of the declared variable.
int what; //A blank variable has been declared.
ptr=&anotherN; //This is the pointer trick. ptr now pointing to address of anotherN
what=*ptr; /*Now what=address_of_anotherN, and anotherN_address=1337. So what=1337.
Whenever we are going to change the value of a variable using a pointer
remember that we need the asteric. First we need "&" as reference address.
then we need the asteric for getting final declaration and the value. */
*ptr=420; /* Remember that anotherN's value was 1337 ? But now it is 420. So we really
can change the value anything by declaring new value. Remember that it will
only change the value of the reference address. So in our case it "anotherN" */
printf("nnn%sn",str); //Print the value of "str"
printf("Value of what: %dn",what);
printf("Value of anotherN: %dn",anotherN);
//Time to go in advanced!
typedef struct pusheax
char add; //It is not a problem using array.
}pUsheax; //The masked name
pUsheax instanc; //Getting instance name of the struct.
pUsheax *mypusheax; //Declaring "pusheax" type pointer. Yes we can have pointer in struct.
mypusheax=&instanc; //Pointing to instanc
//Print the value of "push":
printf("The current value of "push" is: %dn",instanc.push);
mypusheax->push=200; /* Here is tricky? Now we are not changing the value using asteric.
here we are using dash and "greater than" operator to redirect to
declard variable. Data actually going mypusheax and mypusheax
transfering the data to "push". So it shopisticately chage the value.*/
printf("Now "push"= %dnnn",instanc.push);
strcpy(mypusheax->add,"www.pusheax.com"); /*store the string pusheax.com in "add" array.
strcpy is evil. It causes the buffer overflow
if the "add" array gets fill with more than 20
value then stack will get overwriten. Because
strcpy does not check boundary. More secure function
is strncpy() :)*/
printf("ttttt%sn",instanc.add); //We print the current value of add declared array.
printf("ttttt%snnn",mypusheax->add); /* We also can get the value of add instead calling
calling instanc.add */
Compile and Run:
push@pusheax:~/code$ gcc strucptr.c -o strucptr
We need to know programming for being a security resarcher.
Value of what: 1337
Value of anotherN: 420
The current value of "push" is: 100
Now "push"= 200