指针在C中无法正常工作?

吉姆·琼斯

嘿,所以我试图编写一个代码,其中一个人输入一个字符串,然后输入一个较小的第二个字符串。该程序应该在第一个字符串中寻找第二个字符串。我被我使用的指针卡住了。我认为我没有以正确的方式使用它们。在名为search的函数中,我尝试打印应该存储在结构体的指针中的值,以使程序崩溃。我在遇到问题的部分添加了注释行:

#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);
merlin2011

正如评论所提到的,代码的当前状态存在许多问题。除了各种语法问题(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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章