day10 用分文件编译和makefile实现以单词为单位进行字符串倒置,不用数组而是用动态申请的内存。

1

函数代码:

#include"head.h"
char* create(int size){
	char*p=(char*)malloc(size);
	return p;
}
void my_invert(char* str){
	int len=strlen(str);
	int i=0,j=0,k=0;
	while(i

头文件代码:

#include
#include
#include
extern char* create(int );
extern void my_invert(char*);
extern char* my_free(char*);
extern void invert(char*,char*);

主函数代码

#include"head.h"
int main(int argc, const char *argv[])
{
	char*p=create(32);
	printf("请输入字符串");
	gets(p);
	puts(p);
	my_invert(p);
	printf("%s\n",p);
	p=my_free(p);
	return 0;
}

 makefile代码

CC=gcc
CFLAGs=-c -Wall -g
EXE=myprogram
OBJS=$(wildcard *.c)
OBJs=$(patsubst %.c,%.o,$(OBJS))
all:$(EXE)
$(EXE):$(OBJS)
	$(CC)  $^ -o $@ 
clean:
	rm *.o $(EXE)
.PHONY :clean

结果:

ubuntu@ubuntu:invert_str$ make
gcc  main.c fun.c -o myprogram 
main.c: In function ‘main’:
main.c:6:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
  gets(p);
  ^~~~
  fgets
/tmp/ccUwP2Kz.o:在函数‘main’中:
main.c:(.text+0x3b): 警告: the `gets' function is dangerous and should not be used.
ubuntu@ubuntu:invert_str$ ./myprogram 
请输入字符串this is a book
this is a book
koob a si siht
book a is this

你可能感兴趣的:(算法,c#,linux,数据结构,c语言)