嘿所以我正在尝试编写一个人输入字符串的代码,然后输入一个较小的第二个字符串。程序应该在第一个字符串中查找第二个字符串。我坚持使用我正在使用的指针。我不认为我正在以正确的方式使用它们。在名为search的函数中,我尝试打印应该存储在struct中的指针中的值,以使程序崩溃。我在我遇到问题的部分添加了注释行:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct subsequence_struct
{
char* sequence; // the larger sequence being searched
int sizesequence; // size of the array sequence
char* subseq; // the subsequence we are looking for
int sizesubseq; // size of the char array subseq
int* locations; // a pointer to a list of locations where the string in subseq is found in sequence.
int numlocations; // size of the int array locations
} SUBSEQUENCE;
/* function to create space for the sequence,
read in the sequence from stdio,
and return a pointer to the sequence */
char* initseq()
{
SUBSEQUENCE* seq;
seq = (SUBSEQUENCE*)malloc(sizeof(SUBSEQUENCE));
seq->sequence = (char *)malloc(sizeof(char)*100);
printf("Enter the sequence to be searched: ");
fscanf(stdin, "%s", seq->sequence);
seq->sizesequence = strlen(seq->sequence);
return;
}
//on to read in the subsequence and store in subseq
initsubseq(SUBSEQUENCE *input)
{
input = (SUBSEQUENCE*)malloc(sizeof(SUBSEQUENCE));
input->subseq = (char*)malloc(sizeof(char)*10);
printf("Enter the subsequence to search for: ");
scanf("%s", input->subseq);
input->sizesubseq = strlen(input->subseq);
return;
}
printresults(SUBSEQUENCE* input)
{
return 0;
}
//This where I'm having problems
search(SUBSEQUENCE* ptrToInput)
{
printf("This is the sequence: %s\n", ptrToInput->sequence);
printf("This is the subseq: %s", ptrToInput->subseq);
return;
}
int main()
{
SUBSEQUENCE* dna=0;
dna = (SUBSEQUENCE*)malloc(sizeof(SUBSEQUENCE));
initseq(dna);
initsubseq(dna);
search(dna);
printresults(dna);
return 0;
}
我尝试在函数中使用此代码并在程序中进行垃圾打印:
ptrToInput = (SUBSEQUENCE*)malloc(sizeof(SUBSEQUENCE));
ptrToInput->sequence = (char *)malloc(sizeof(char)*100);
ptrToInput->subseq = (char *)malloc(sizeof(char)*10);
printf("This is the sequence %s\n", ptrToInput->sequence);
printf("This is the subseq %s", ptrToInput->subseq);
答案 0 :(得分:1)
正如评论所提到的,代码的当前状态存在各种问题。除了各种语法问题(C编译器大多会原谅),主要的功能问题是你似乎在本地函数中声明,分配,然后忽略内存。
您可以通过传入分配的指针,然后操纵它而不是在每个init*
函数中声明一个新指针来解决此问题。
以下代码将使用gcc
和g++
进行编译,并输出两个用户输入而不是NULL
或随机乱码。
请注意,它代表了一系列更正,但不会搜索子序列,比原始代码更多。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct subsequence_struct {
char* sequence; // the larger sequence being searched
int sizesequence; // size of the array sequence
char* subseq; // the subsequence we are looking for
int sizesubseq; // size of the char array subseq
int* locations; // a pointer to a list of locations where the string in subseq is found in sequence.
int numlocations; // size of the int array locations
} SUBSEQUENCE;
// You already allocated in main, no need to allocate again.
void initseq(SUBSEQUENCE* seq)
{
seq->sequence = (char *)malloc(sizeof(char)*100);
printf("Enter the sequence to be searched: ");
fscanf(stdin, "%s", seq->sequence);
seq->sizesequence = strlen(seq->sequence);
}
// You already allocated in main, no need to allocate again.
void initsubseq(SUBSEQUENCE *input)
{
input->subseq = (char*)malloc(sizeof(char)*10);
printf("Enter the subsequence to search for: ");
scanf("%s", input->subseq);
input->sizesubseq = strlen(input->subseq);
return;
}
// This function currently does not search, but it does print the user's inputs.
void search(SUBSEQUENCE* input)
{
printf("This is the sequence: %s\n", input->sequence);
printf("This is the subseq: %s\n", input->subseq);
return;
}
int main(){
SUBSEQUENCE* dna=0;
dna = (SUBSEQUENCE*)malloc(sizeof(SUBSEQUENCE));
initseq(dna);
initsubseq(dna);
search(dna);
// Free various pieces that were allocated.
free(dna->sequence);
free(dna->subseq);
free(dna);
return 0;
}