我的计划出了什么问题?

时间:2014-01-06 07:13:13

标签: c

该程序应该比较字符串并按字母顺序放置它们,但最后的printf语句打印出垃圾......我在哪里错了?
我已经使用了一个指向字符串的指针数组并在开始时声明它们,我甚至尝试使用temp作为数组而不是指针stil不能正常工作

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
    int j;
    int i;
        char *temp;
        char *string[5] = {"ibrahim","laura","peter","degea"};
        for ( i = 0; i< 4;i++)
        printf("%s\n", string[i]);  //allocating memory
        for( i = 0; i< 10 ;i++)
        string[i] = (char*)malloc (30 * sizeof(char));               
        temp = (char*)malloc(30*sizeof(char));               
        for ( i=0; i< 3; i++)
        for(j =i+1; j<4; j++)
        {
           if(strcmp(string[i], string[j]) > 0)
           {
             strcpy(temp, string[i]);
             strcpy(string[i], string[j]);
             strcpy(string[j], temp);

           }
        }
        for (i = 0; i< 4; i++)
        {
            printf("%s\n",string[i]);
            puts("\n");
        }
        free(string);
        free(temp);
        return 0;
    }

4 个答案:

答案 0 :(得分:1)

string[i] = (char*)malloc (30 * sizeof(char));

这会覆盖现有的初始化值。

修改

for( i = 0; i< 10 ;i++)

temp = (char*)malloc(30*sizeof(char));

什么?你是在一个指针上多次分配内存10次?

编辑2:

原始帖子丢失(阅读已编辑)。当你读到这个答案的时候,Jonathan Leffler先生和这张海报在这个世界上看起来都像个绝对的傻瓜。

答案 1 :(得分:1)

代码最初销毁了它的数据数组(通过分配新的指针)。现在它似乎通过分配temp四次来释放内存(但Jeevan的编辑以缩进形式恢复旧代码)。旧代码为字符串分配空间但从未初始化它们(事先抛弃了数组中的值)。

NB:在您阅读评论时,对代码的任何评论都很容易失效 - 问题中的代码可能已经更改。


for (i = 0; i < 10; i++)
    string[i] = malloc(30);

此代码践踏string数组的范围(维度为5)。它也会丢失最初在数组中的数据。


您需要使用strcmp()来比较字符串,但您只需要交换指针而不是使用strcpy()

// allocating memory声明旁边的评论printf()也具有误导性。


这有机会工作。请注意,没有内存分配:

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

int main(void)
{
    int j;
    int i;
    char *string[] = { "ibrahim", "laura", "peter", "degea" };

    for (i = 0; i < 4; i++)
        printf("%s\n", string[i]);

    for (i = 0; i < 3; i++)
    {
        for (j = i+1; j < 4; j++)
        {
            if (strcmp(string[i], string[j]) > 0)
            {
                char *temp = string[i];
                string[i] = string[j];
                string[j] = temp;
            }
        }
    }

    for (i = 0; i < 4; i++)
        printf("%s\n",string[i]);

    return 0;
}

输出:

ibrahim
laura
peter
degea
degea
ibrahim
laura
peter

评论:不要尝试使用此排序算法对数千个字符串进行排序。

答案 2 :(得分:0)

您正在使用malloc覆盖现有的字符串数组内容,这也是10次。 请在代码中使用大括号以便更好地理解

  for( i = 0; i< 10 ;i++)
    string[i] = (char*)malloc (30 * sizeof(char));               
    temp = (char*)malloc(30*sizeof(char));      

可以写成

  for( i = 0; i< 10 ;i++) // why to allocate memory ten times, that too overwriting
  {
    string[i] = (char*)malloc (30 * sizeof(char));               
  }

  temp = (char*)malloc(30*sizeof(char));      

答案 3 :(得分:0)

当你宣布: char * string [5] = { “易卜拉欣” “劳拉”, “彼得”, “degea”};

指针* string [0]到* string [3]分配给内存中的地址,其中将存储init值“ibrahim”,“laura”......

当你使用malloc函数作为指针字符串[i] =(char *)malloc(30 * sizeof(char)); 指针* string [0]到* string [4]将被分配给内存中的另一个地址。所以这些地址的值与你初始化的值不同(ibrahim,laura)。

你应该使用静态数组而不是动态如下

char temp;

char string [5] = { “易卜拉欣” “劳拉”, “彼得”, “degea”};

删除所有malloc,free函数。

相关问题