struct,typedef,array and pointer [all togther?]

The site moved to root domain where all post are imported. Please go to

This post is part of . Such as struct, pointer, typedef example. I have did some more advanced things with struct:

#include <stdio.h>
#include <string.h>

int main()

//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

int push;
char add[20]; //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
instanc.push=100; //push=100

//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,""); /*store the string 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[20] 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
push@pusheax:~/code$ ./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


Leave a Reply

Your email address will not be published. Required fields are marked *