C - 随机字符数组排序



我在编译程序时遇到问题,我不确定我的逻辑和/或语法是否正确。我正在尝试做的是制作一个大小(由用户定义)字符串的数组,每个字符串有 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("%sn", 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] = '';
    return *word; //returns the string
}

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

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

#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("%sn", 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] = '';
    return word; //returns the string
}
你必须

研究更好的指针,你必须使用string.h来表示strcpy函数,使用stdlib.h来表示malloc。

我看到的问题:

改进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] = '';
   /// 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] = '';
   // 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语句

你需要string.h才能strdupstdlib.h才能rand。加

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

在文件的顶部。

最新更新