File tree Expand file tree Collapse file tree 5 files changed +46
-19
lines changed
Expand file tree Collapse file tree 5 files changed +46
-19
lines changed Original file line number Diff line number Diff line change 1+ PROJECT (TEST )
2+ CMAKE_MINIMUM_REQUIRED (VERSION 2.9)
3+ INCLUDE_DIRECTORIES (./include )
4+ AUX_SOURCE_DIRECTORY (./src DIR_SRC)
5+ ADD_EXECUTABLE (./bin ${DIR_SRC} )
Original file line number Diff line number Diff line change @@ -4,3 +4,4 @@ mkdir build
44cd build
55cmake ..
66make
7+ ./bin
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include <string.h>
4+ void strval (char * * str1 ,const char * str2 ,int type ){
5+ switch (type )
6+ {
7+ case 1 : {
8+ * str1 = (char * )malloc (strlen (str2 + 1 ));
9+ sprintf (* str1 ,"%s" ,str2 );
10+ printf ("case 1 :%s\n" ,* str1 );
11+ break ;
12+ }
13+ case 2 : {
14+ * str1 = (char * )realloc (* str1 ,strlen (* str1 )+ strlen (str2 )+ 1 );
15+ sprintf (* str1 ,"%s%s" ,* str1 ,str2 );
16+ printf ("case 2 :%s\n" ,* str1 );
17+ }
18+ break ;
19+ case 3 : {
20+ free (* str1 );
21+ * str1 = (char * )malloc (strlen (str2 + 1 ));
22+ sprintf (* str1 ,"%s" ,str2 );
23+ printf ("case 3 :%s\n" ,* str1 );
24+ break ;
25+ }
26+ }
27+ }
28+ struct test {
29+ int type [2 ];
30+ char * name ;
31+ struct test * next ;
32+ };
33+ struct test * add (){
34+ struct test * temp = (struct test * )malloc (sizeof (struct test ));
35+ return temp ;
36+ }
Original file line number Diff line number Diff line change 1- #include <stdio.h>
2- #include <stdlib.h>
3- #include <string.h>
4- void strinit (char * * str1 ,const char * str2 ){
5- * str1 = (char * )malloc (strlen (str2 + 1 ));
6- sprintf (* str1 ,"%s" ,str2 );
7- }
8- struct test {
9- int type [2 ];
10- char * name ;
11- struct test * next ;
12- };
13- struct test * add (){
14- struct test * temp = (struct test * )malloc (sizeof (struct test ));
15- return temp ;
16- }
1+ #include "../include/head.h"
172int main (){
183 struct test * test1 = add ();
194 test1 -> type [0 ]= 1 ;
20- // test1->name=(char *)malloc(12 );
21- //sprintf( (test1->name),"hello world");
22- strinit (& (test1 -> name ),"hello world" );
5+ strval ( & ( test1 -> name ), "hello " , 1 );
6+ strval ( & (test1 -> name ),"world" , 2 );
7+ strval (& (test1 -> name ),"goodbye world" , 3 );
238 printf ("name:%s\nnum:%d\n" ,(test1 -> name ),(test1 -> type [0 ]));
249 return 0 ;
2510}
You can’t perform that action at this time.
0 commit comments