Vous êtes sur la page 1sur 7

Pointers

void main() { int number = 0; int *pointer = NULL; /* A pointer that can point to type int

*/

number = 10; printf("\n number's address: %p", &number); printf("\n number's value: %d\n\n", number); pointer = &number; /* Store the address of number in pointer */ printf("pointer's address: %p", &pointer); /* Output the address */ printf("\npointer's size: %d bytes", sizeof(pointer)); /* Output the size */ printf("\npointer's value: %p", pointer); /* Output the value (an address) */ printf("\nvalue pointed to: %d\n", *pointer); /* Value at the address */

int main() { int integerVariable; int *integerPointer; integerVariable = 2; printf("Thing %d\n", integerVariable); integerPointer = &integerVariable; *integerPointer = 3; printf("Thing %d\n", integerVariable); printf("Thing %d\n", *integerPointer); return (0); } main () { int i; int * ia; i = 10; ia = &i; printf ("The address of i is %8u \n", ia); printf ("its value is %d\n", i); printf ("its value is %d\n", *ia); *ia = 50; printf ("the value of i is %d\n", i); } int main(void) { int target, source; int *m; source = 10; m = &source; target = *m; printf("%d", target); } return 0;

Functions main() { int num[10],i,n,searchno; clrscr(); printf("enter num to be searched"); scanf("%d",&searchno); printf("enter n"); scanf("%d",&n); printf("enter number in list"); printf("the entered numbers are:"); for(i=0;i<n-1;i++) { scanf("%d",&num[i]); } for(i=0;i<n-1;i++) { if(num[i]==searchno) printf("the searchno is %d , loc is %d",num[i],i); else printf("not found"); break; } getch(); } void main(void) { int hour,min,sec,i; clrscr(); printf("ENTER HOURS:"); scanf("%d",&hour); printf("ENTER MINUTES:"); scanf("%d",&min); printf("ENTER SECONDS:"); scanf("%d",&sec); clrscr(); for(i=1;i<=I+1;i++) { sec=sec+1; if(sec==60) { sec=0; min=min+1; } if(min==60) { min=0; hour=hour+1; } if(hour==24) hour=0; clrscr(); gotoxy(10,25); printf("%02d:%02d:%02d",hour,min,sec); sleep(1); } getch(); }

Functions with no arguments and no return value. void printline()

{ int i; printf("\n"); for(i=0;i<30;i++) { printf("-"); } printf("\n"); } void main() { clrscr(); printf("Welcome to function in C"); printline(); printf("Function easy to learn."); printline(); getch(); }

Functions with arguments and no return value


void add(int x, int y) { int result; result = x+y; printf("Sum of %d and %d is %d.\n\n",x,y,result); } void main() { clrscr(); add(30,15); add(63,49); add(952,321); getch(); }

Functions with arguments and return value


int add(int x, int y) {

int result; result = x+y; return(result); } void main() { int z; clrscr(); z = add(952,321); printf("Result %d.\n\n",add(30,55)); printf("Result %d.\n\n",z); getch(); } Functions with no arguments but returns value int send() { int no1; printf("Enter a no : "); scanf("%d",&no1); return(no1); } void main() { int z; clrscr(); z = send(); printf("\nYou entered : %d.", z); getch(); }

Functions that return multiple values


void calc(int x, int y, int *add, int *sub) { *add = x+y; *sub = x-y; } void main() {

int a=20, b=11, p,q; clrscr(); calc(a,b,&p,&q); printf("Sum = %d, Sub = %d",p,q); getch(); }

Structures
Structures in C are used to encapsulate, or group together different data into one object. You can define a Structure as shown below: struct object { char id[20]; int xpos; int ypos; };
Structure members can be initialized when you declare a variable of your structure: struct object player1

= {player1, 0, 0}; The above declaration will create a struct object called player1 with an id equal to player1, xpos equal to 0, and ypos equal to 0. To access the members of a structure, you use the . (scope resolution) operator. Shown below is an example of how you can accomplish initialization by assigning values using the scope resolution operator: Sample Code 1. 2. 3. 4. struct object player1; player1.id = player1; player1.xpos = 0; player1.ypos = 0;

Functions and Structures


Since structures are of custom data types, functions can return structures and also take them as arguments. Keep in mind that when you do this, you are making a copy of the structure and all it's members so it can be quite memory intensive. To return a structure from a function declare the function to be of the structure type you want to return. In our case a function to initialize our object structure might look like this: Sample Code 1. 2. 3. 4. 5. 6. 7. 8. return newobj; strcpy(newobj.id, name); newobj.xpos = xpos; newobj.ypos = ypos; struct object createobj(char id[], int xpos, int ypos) { struct object newobj;

9.

Pass Structure to a Function


Let us now learn to pass a structure to a function. As an example let us use a function that prints members of the structure passed to it: Sample Code 1. 2. 3. 4. 5. 6. } void printobj(struct object obj) { printf(name: %s, , obj.id); printf(x position: %d, , obj.xpos); printf(y position: %d, obj.ypos); printf(n);

For completeness we shall include the full source of the above examples so you may see how it all fits together. object1.c: Sample Code 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. struct object createobj(char id[], int xpos, int ypos); 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. struct object createobj(char id[], int xpos, int ypos) { } printobj(player1); printobj(enemy1); struct object player1 = createobj("player1", 0, 0); struct object enemy1 = createobj("enemy1", 2, 3); void main() { void printobj(struct object obj); struct object { char id[20]; int xpos; int ypos; }; #include <stdio.h> #include <stdlib.h>

24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. } }

struct object newobj; strcpy(newobj.id, id); newobj.xpos = xpos; newobj.ypos = ypos; return newobj;

void printobj(struct object obj) { printf("name: %s, ", obj.id); printf("x position: %d, ", obj.xpos); printf("y position: %d", obj.ypos); printf("n");

Vous aimerez peut-être aussi