1. Pure of array
========================
#include <stdio.h>
#include <stdlib.h>
int setFirst(char n[]){
n[0] = 'l';
n[1] = 'a';
n[2] = 'd';
n[3] = 'y';
return 0;
}
int setSecond(char n[]){
n[4] = ' ';
n[5] = 'g';
n[6] = 'a';
n[7] = 'g';
n[8] = 'a';
n[9] = '\0';
return 0;
}
int main(int c){
system("color 5f");
char name[10];
setFirst(name);
setSecond(name);
printf("%s",name);
return 0;
}
2. Using pointer and dma
==============================
#include <stdio.h>
#include <stdlib.h>
int setFirst(char n[]){
n[0] = 'l';
n[1] = 'a';
n[2] = 'd';
n[3] = 'y';
return 0;
}
int setSecond(char n[]){
n[4] = ' ';
n[5] = 'g';
n[6] = 'a';
n[7] = 'g';
n[8] = 'a';
n[9] = '\0';
return 0;
}
int main(int c){
system("color 5f");
char* name;
name = (char *)malloc(1*sizeof(char));
setFirst(name);
setSecond(name);
printf("%s",name);
return 0;
}
3. argument pointer
=======================
#include <stdio.h>
#include <stdlib.h>
int setFirst(char *n){
n[0] = 'l';
n[1] = 'a';
n[2] = 'd';
n[3] = 'y';
return 0;
}
int setSecond(char n[]){
n[4] = ' ';
n[5] = 'g';
n[6] = 'a';
n[7] = 'g';
n[8] = 'a';
n[9] = '\0';
return 0;
}
int main(int c){
system("color 5f");
char* name;
name = (char *)malloc(1*sizeof(char));
setFirst(name);
setSecond(name);
printf("%s",name);
return 0;
}
4. Use strcpy and strcat
========================
#include <stdio.h>
#include <stdlib.h>
int setFirst(char *n){
const char* name = "lady";
strcpy(n, name);
return 0;
}
int setSecond(char n[]){
const char* name = " gaga";
strcat(n, name);
return 0;
}
int main(int c){
system("color 5f");
char* name;
name = (char *)malloc(1*sizeof(char));
setFirst(name);
setSecond(name);
printf("%s",name);
return 0;
}
No comments:
Post a Comment