Advertisement
B1KMusic

C struct example

Apr 17th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct{
  6.   char *name;
  7.   int age;
  8. } person;
  9.  
  10. void person_change_name(person *p, char *new_name){
  11.   p->name = calloc(sizeof(char), strlen(new_name));
  12.   strcpy(p->name, new_name);
  13. }
  14.  
  15. void person_profile(person *p){
  16.   printf("Name: %s\nAge: %i\n", p->name, p->age);
  17. }
  18.  
  19. int main(){
  20.   person p = {"John", 32};
  21.   person_profile(&p);
  22.   person_change_name(&p, "Bobby");
  23.   person_profile(&p);
  24.  
  25.   return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement