简体   繁体   English

字符串数组打印错误的值

[英]array of strings printing wrong values

I trying to make a program that calculates proper prefixes and proper suffixes , and then compare the sets , and then return the array containing values representing number of matched pairs , This could be used later in KMP algorithm . 我试图制作一个程序,计算适当的前缀和适当的后缀,然后比较这些集合,然后返回包含表示匹配对数的值的数组,这可以稍后在KMP算法中使用。 But the problem is prefixes and suffixes array give wrong values . 但是问题是前缀和后缀数组给出错误的值。 Even after appending a new element at new index, it replaces the all value in the array with the new element . 即使在将新元素附加到新索引之后,它也会用new element替换数组中的所有值。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int* lps(char *,int );

int main()
{

char *pat = "abababca";

int *ptr ;
ptr = lps(pat,strlen(pat));

printf("\n***LPS***\n");


}

int * lps (char *p,int s)
{


char *prefixes[s] ;
char *suffixes[s] ;
char tmp1[s] , tmp2[s];
int i , j , k , c1 , c2 , c;

for (i = 0 ; i < s ; i ++)
{
    printf("\n\n*** --- Creating Prefixes and Suffixes for i = %d --- ***",i);

    c1 = 0 ;
    //create prefixes
    for (j = 0 ; j < i; j++)
    {
        for (k =0 ; k <= j; k++)
        {
            tmp1[k]=*(p+k);
            printf("\n *(p+%d)= %c , tmp1[%d]=%c",k,*(p+k),k,tmp1[k]);
        }

        tmp1[k]='\0';
        printf("\nprefixes[0]:%s",prefixes[0]);
        prefixes[c1] = tmp1;
        //strcpy(prefixes[c1], tmp1);

        printf("\ncurrently added %s to prefixes at %d and prefixes[%d]= %s\n ",tmp1,c1,c1,prefixes[c1]);
        c1++;
    }

    //print prefixes
    for (k = 0; k<c1; k++)
    {
        printf("\tprefixes[%d] = %s",k,prefixes[k]);
    }
    printf("\n");



    //create suffixes
    c2 = 0;
    for (j = 1 ; j <= i; j++)
    {
        for (k = j ; k  <= i; k++)
        {
            tmp2[k-j] = *((p+k));
            printf("\n *(p+%d)= %c , tmp2[%d]=%c",k,*(p+k),k-j,tmp2[k-j]);
        }

         tmp2[k-j]='\0';
         suffixes[c2] = tmp2 ;
        // strcpy(suffixes[c2], tmp2);

         printf("\ncurrently added %s to suffixes at %d and suffixes[%d]= %s\n",tmp2,c2,c2,suffixes[c2]);
        c2++;
    }

     //prinf suffixes
    for (k = 0; k<c2; k++)
    {
        printf("\tsuffixes[%d] = %s",k,suffixes[k]);
    }
    printf("\n");
    //compare the prefixes and suffixes

    c = 0 ;
    for (j = 0; j < c1; j++)
    {
        for(k=0 ; k < c2 ; k++)
        {
            printf("\nprefixes[%d] = %s , suffixes[%d] = %s\n ",j,prefixes[j],k,suffixes[k]);

            if (strcmp(prefixes[j], suffixes[k])==0)
            {
                c = c + 1 ;
            }
        }
    }

  }
  }

OUTPUT (Some part of OUTPUT) :- 输出(OUTPUT的某些部分):-

prefixes[0] = ab    prefixes[1] = ab   //it should be prefixes[0] = a   prefixes[1] = ab

The problem is that you aren't allocating any strings. 问题是您没有分配任何字符串。 The only strings that you have in lps are tmp1 and tmp2 . lps中仅有的字符串是tmp1tmp2 You then make assignments like this: 然后,您可以进行如下分配:

prefixes[c1] = tmp1;

That assigns a pointer but does not copy the contents of the string. 这会分配一个指针,但不会复制字符串的内容。 You'll end up with every entry in prefixes pointing to the same string, tmp1 . 最后,您将获得prefixes指向相同字符串tmp1每个条目。 And likewise for suffixes . suffixes也是如此。

You'll need to use malloc and strcpy to make new string instances. 您将需要使用mallocstrcpy来创建新的字符串实例。

In the code you have commented out calls to strcpy . 在代码中,您已注释掉对strcpy I suspect you tried these and encountered runtime errors. 我怀疑您尝试了这些,并且遇到了运行时错误。 Those runtime errors were because you did not allocate any memory. 这些运行时错误是因为您没有分配任何内存。 The corrected code would look like: 更正后的代码如下所示:

prefixes[c1] = malloc(strlen(tmp1)+1);
strcpy(prefixes[c1], tmp1);

And similarly for suffixes . suffixes

In production quality code you'd include error checking. 在生产质量代码中,您将包括错误检查。 And you'd want to make sure that you call free() on any pointer returned by the calls malloc() , once you had finished with it. 并且,您要确保在完成malloc()返回的任何指针后调用free()

I'd also question the use of C variable length arrays, VLAs. 我也会质疑使用C可变长度数组VLA。 In your code, prefixes , suffixes , tmp1 and tmp2 are all VLAs. 在你的代码, prefixessuffixestmp1tmp2都是沃拉斯。 Using VLAs can readily lead to stack overflow if you have large array dimensions. 如果阵列尺寸较大,使用VLA很容易导致堆栈溢出。 My instincts say that heap allocation is what you need here. 我的直觉是您需要在这里分配堆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM