【问题标题】:random character array sort随机字符数组排序
【发布时间】:2016-02-07 00:15:04
【问题描述】:

我在编译我的程序时遇到问题,我不确定我的逻辑和/或语法是否正确。我要做的是制作一个大小为(由用户定义)的字符串数组,每个字符串有 20 个字符。

//Melissa P. 
//University of Massachusetts Dartmouth
//CIS362
//2-6-2016

#include <stdio.h>

#define MIN 0 //For the random number generator
#define MAX 51 //so it can properly choose randomly a letter from my given array in getRandomString function

int getNum(void);
char * getRandomString(void);

int main (void)
{
    int input = 0;
    int i = 0;
    int j = 0;
    char word[20]; //placeholder for the random string result


    printf("How many strings do you want in the array? "); //questions the user for amount of 20 character strings
    scanf("%d", &input); 
    const char *array[input]; //pointer array of size defined by user

    for (i = 0; i < input; i++)
    {
        *word = getRandomString(); //gets the 20 character string from the function
        array[i] = *word; //and puts it in each space of the array
    }

    for (i = 0; i < input; i++)
    {
        printf("%s\n", array[i]); //prints the array
    }

}

int getNum() //function to get a random number between 0 and 51.
{
    int num;
    num = rand()% ((MAX + 1) - MIN) + MIN;
    return num;
}

char * getRandomString(void)
{
    char word[20]; //declares the array for the random word to go in.
    int num = 0;
    int i = 0;
    char letterArray[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
                      'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
                      'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
                      'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 
                      'W', 'X', 'Y', 'Z'}; //array of all lower case and upper case letters

    for (i = 0; i < 20; i++)
    {
        num = getNum(); //gets a random number seed
        word[i] = letterArray[num]; //fills the word array with a random letter
    }
    word[i] = '\0';

    return *word; //returns the string
}

我得到的错误是:“赋值从没有强制转换的指针中生成整数”这发生在 main 和 getRandomString 方法中。 谢谢!

【问题讨论】:

  • 在 for 循环之后 i 的值为 20 并且 word[i] 不存在。然后你返回 *word。只需返回单词。
  • 在 for 循环之后 i 的值为 19,因为在 for 语句中 i
  • No 在循环中它执行 i++ 并且当 i

标签: c arrays string character


【解决方案1】:

你的代码有很多错误,我已经纠正了你的错误,试试:

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

#define MIN 0 //For the random number generator
#define MAX 51 //so it can properly choose randomly a letter from my given array in getRandomString function

int getNum(void);
char * getRandomString(void);

int main (void)
{
    int input = 0;
    int i = 0;
    int j = 0;
    char* word; //placeholder for the random string result


    printf("How many strings do you want in the array? "); //questions the user for amount of 20 character strings
    scanf("%d", &input); 
    char **array = malloc(input * sizeof(char*)); //pointer array of size defined by user

    for (i = 0; i < input; i++)
    {
        array[i] = malloc(21);
    }

    for (i = 0; i < input; i++)
    {
        word = getRandomString(); //gets the 20 character string from the function
        strcpy(array[i],word); //and puts it in each space of the array
        free(word);
    }

    for (i = 0; i < input; i++)
    {
        printf("%s\n", array[i]); //prints the array
    }

    //free memory
    for (i = 0; i < input; i++)
    {
        free(array[i]);
    }

    free(array);
}

int getNum() //function to get a random number between 0 and 51.
{
    int num;
    num = rand()% ((MAX + 1) - MIN) + MIN;
    return num;
}

char * getRandomString(void)
{
    char* word = malloc(21); //declares the array for the random word to go in.
    int num = 0;
    int i = 0;
    char letterArray[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
                      'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
                      'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
                      'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 
                      'W', 'X', 'Y', 'Z'}; //array of all lower case and upper case letters

    for (i = 0; i < 20; i++)
    {
        num = getNum(); //gets a random number seed
        word[i] = letterArray[num]; //fills the word array with a random letter
    }
    word[i] = '\0';

    return word; //returns the string
}

你必须学习更好的指针,你必须使用 string.h 用于 strcpy 函数和 stdlib.h 用于 malloc。

【讨论】:

    【解决方案2】:

    我看到的问题:

    改进建议getRandomString

    getRandomString 有几个问题。

    char* getRandomString(void)
    {
       char word[20];
       int num = 0;
       int i = 0;
       char letterArray[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
          'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
          'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
          'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 
          'W', 'X', 'Y', 'Z'};
    
       /// Problem ///
       /// When you have an array of 20 characters for a string
       /// you can fill at most 19 characters. The 20-th character
       /// needs to be the null character.
       for (i = 0; i < 20; i++)
       {
          num = getNum();
          word[i] = letterArray[num];
       }
    
       /// Problem ///
       /// By the time you are here, i is 20, one more than the highest valid index.
       /// You need to terminate the for loop with the conditional 
       /// i < 19
       word[i] = '\0';
    
       /// Problem ///
       /// *word evaluates to the first character of the string.
       /// The type it evaluates to is char, which does not match the
       /// return type of the function. Changing to "return word;" will
       /// be syntactically correct but that will be a problem. You will
       /// be returning a pointer to a string which lives only as long
       /// the function lives. The pointer will be a dangling pointer in
       /// the calling function.
       return *word;
    }
    

    你可以改成:

    char* getRandomString(void)
    {
       char word[20];
       int num = 0;
       int i = 0;
       char letterArray[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
          'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
          'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
          'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 
          'W', 'X', 'Y', 'Z'};
    
       // Stop the loop when i is 19
       for (i = 0; i < 19; i++)
       {
          num = getNum();
          word[i] = letterArray[num];
       }
       word[i] = '\0';
    
       // Return a string that is a copy of word.
       // The pointer will be valid in the calling function. 
       return strdup(word);
    }
    

    以上版本的getRandomString 返回一个指向动态分配内存的指针。在调用函数中,您将不得不释放内存。

    改进建议main

    从以下位置删除const

    const char *array[input];
    

    做起来

    char *array[input];
    

    main 中删除word 的定义。你不需要它。捕获从getRandomString 返回的值所需的只是:

    for (i = 0; i < input; i++)
    {
       array[i] = getRandomString();
    }
    

    在函数结束前添加以下代码块来释放内存。

    for (i = 0; i < input; i++)
    {
       free(array[i]);
    }
    

    添加必要的#include 语句

    对于strdup,您需要string.h,对于rand,您需要stdlib.h。添加

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

    在文件的顶部。

    【讨论】:

      猜你喜欢
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多