我正在编写一个代码来计算绝地名,它是名字和姓氏的组合。我已经编写了整个代码并在while循环中出现分段错误。
以下是代码。它分为头文件和C文件:
结构.h
//void jediName(char *first_name, char *last_name, char buffer[10]);
//void jediName(struct Names Name_Param);
//void * allocate(unsigned int size);
//void * deallocate(void *, int size);
int heap_usage = 0;
struct Names{
char *first_name;
char *last_name;
char *jedi_name;
};
struct Names *name;
void jediName(struct Names *Name_Param);
void * allocate(unsigned int size);
void * deallocate(void *, int size);
程序.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Structures.h"
int main(){
// char fname[10];
// char lname[10];
// char buff[10]= "";
char buffer1[250];
char buffer2[250];
FILE * fp;
/*
printf("Enter the first & last name : n");
scanf("%s %s", buffer1, buffer2 );
fp = fopen("names.txt", "a");
fprintf(fp, "n%s %s", buffer1, buffer2);
fclose(fp);
*/
fp = fopen("names.txt","r");
name->first_name == allocate(10);
name->last_name == allocate(10);
name->jedi_name == allocate(10);
while(!feof(fp)){
fscanf(fp, "%s %s", name->first_name, name->last_name);
jediName(name);
// jediName(name->first_name, name->last_name, name->jedi_name);
printf("%s %s %s", name->first_name, name->last_name);
}
deallocate(name->first_name, 10);
deallocate(name->last_name, 10);
deallocate(name->jedi_name, 10);
fclose(fp);
return 0;
}
/*
void jediName(char *first_name, char *last_name, char buffer[10]){
if(strlen(first_name)<2 || strlen(last_name)<3)
printf("Name of %s %s is too short to compute a jedi namen", first_name, last_name);
else{
buffer[0] = last_name[0];
buffer[1] = last_name[1];
buffer[2] = last_name[2];
buffer[3] = first_name[0];
buffer[4] = first_name[1];
printf("Jedi Name for %s %s is %sn", first_name, last_name, buffer);
}
return;
}
*/
void jediName(struct Names *Name_Param){
if(strlen(Name_Param->first_name)<2 || strlen(Name_Param->last_name)<3)
printf("Name of %s %s is too short to compute a jedi namen", Name_Param->first_name, Name_Param->last_name);
else{
Name_Param->jedi_name[0] = Name_Param->last_name[0];
Name_Param->jedi_name[1] = Name_Param->last_name[1];
Name_Param->jedi_name[2] = Name_Param->last_name[2];
Name_Param->jedi_name[3] = Name_Param->first_name[0];
Name_Param->jedi_name[4] = Name_Param->first_name[1];
printf("Jedi Name for %s %s is %sn", Name_Param->first_name, Name_Param->last_name, Name_Param->jedi_name);
}
return;
}
void * allocate(unsigned int size){
heap_usage = heap_usage + size;
printf("The current heap size after heap allocation is %dn", heap_usage);
void *heapMem = malloc(size);
if(heapMem == NULL)
printf("Pointer is NULLn");
else
printf("Pointer is not NULLn");
return heapMem;
}
void * deallocate(void *heapMem, int size){
heap_usage = heap_usage - size;
printf("The current heap size after heap deallocation is %dn", heap_usage);
free(heapMem);
heapMem = NULL;
return NULL;
}
当您尝试取消引用name
时,您正在调用未定义的行为,因为您没有为其分配任何空间。 你在name
中有一个指针,它不指向任何地方(我相信它实际上是初始化为 0,因为它是静态的(。 在main
中,您必须执行类似 name = malloc(sizeof *name);
的操作,或者您可以将声明更改为 struct Names name;
再看一眼,name
只用在main
,所以只要在那里声明一下。 没有必要在更广泛的范围内宣布它。
int main(void)
{
// no point in dynamically allocating memory in this case. You don't need
// much and you know exactly how much you need (just 1 struct)
struct Names name;
// but if you want to dynamically allocate it..
// struct Names* name = malloc(sizeof *name);
....
// change all your "name->" to "name." if you did not malloc
// if you did not malloc, you must pass the address of name
// to jediName
// jediName(&name);
// .. do you work
// if you used malloc above, don't forget to free your memory
// free(name);
return 0;
}
另请参阅为什么"while ( !feof (file( ("总是错误的?
非常感谢您的帮助。代码中的主要问题是缺少名称的内存分配,"=="以及在调试代码时编写的其他一些额外代码。
以下是最终的 C 文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Structures.h"
int main(){
char buffer1[250];
char buffer2[250];
FILE * fp;
printf("Enter the first & last name : n");
scanf("%s %s", buffer1, buffer2 );
fp = fopen("names.txt", "a");
fprintf(fp, "n%s %s", buffer1, buffer2);
fclose(fp);
fp = fopen("names.txt","r");
name = allocate(10);
name->first_name = allocate(10);
name->last_name = allocate(10);
name->jedi_name = allocate(10);
while(!feof(fp)){
fscanf(fp, "%s %s", name->first_name, name->last_name);
jediName(name);
}
deallocate(name->first_name, 10);
deallocate(name->last_name, 10);
deallocate(name->jedi_name, 10);
deallocate(name, 10);
fclose(fp);
return 0;
}
void jediName(struct Names *Name_Param){
if(strlen(Name_Param->first_name)<2 || strlen(Name_Param->last_name)<3)
printf("Name of %s %s is too short to compute a jedi namen", Name_Param->first_name, Name_Param->last_name);
else{
Name_Param->jedi_name[0] = Name_Param->last_name[0];
Name_Param->jedi_name[1] = Name_Param->last_name[1];
Name_Param->jedi_name[2] = Name_Param->last_name[2];
Name_Param->jedi_name[3] = Name_Param->first_name[0];
Name_Param->jedi_name[4] = Name_Param->first_name[1];
printf("Jedi Name for %s %s is %sn", Name_Param->first_name, Name_Param->last_name, Name_Param->jedi_name);
}
return;
}
void * allocate(unsigned int size){
heap_usage = heap_usage + size;
printf("The current heap size after heap allocation is %dn", heap_usage);
void *heapMem = malloc(size);
if(heapMem == NULL)
printf("Pointer is NULLn");
else
printf("Pointer is not NULLn");
return heapMem;
}
void * deallocate(void *heapMem, int size){
heap_usage = heap_usage - size;
printf("The current heap size after heap deallocation is %dn", heap_usage);
free(heapMem);
heapMem = NULL;
return NULL;
}