C: 生成随机字符串并将其添加到结构体的函数 [英] C: Function to generate random string and add it to struct

查看:34
本文介绍了C: 生成随机字符串并将其添加到结构体的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Java/C# 的一些经验,我正在努力了解 C 的某些部分.我有一个结构数组.目前,这些结构只有一个成员(我将继续添加其他成员)——一组字符.这个数组是一个 6 个字符的字符串 - 两个字母,四个数字(例如 XY1234).

Coming from some experience with Java/C#, I'm struggling to get my head around some parts of C. I have an array of structs. These structs, at the moment, have only one member (I'll be adding others down the track) - an array of chars. This array is a string of 6 characters - two letters, four digits (e.g. XY1234).

以下代码生成我正在寻找的 objectCode:

The follow code produces the objectCode I'm looking for:

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

#define LETTERS_IN_ALPHABET 26

typedef struct object {
    char objectCode[6];
} object_t;

void GetRandomSeed() {
    srand((unsigned)time(NULL));
}

char RandomLetter() {
    return 'A' + rand() % LETTERS_IN_ALPHABET;
}

int RandomDigit() {
    return rand() % 10;
}

int main() {

    GetRandomSeed();

    object_t object1;

    for (int i = 0; i < 2; i++) {
        object1.objectCode[i] = RandomLetter();
    }

    for (int i = 2; i < 6; i++) {
        object1.objectCode[i] = '0' + RandomDigit();
    }

    // Print objectCode string to screen
    for (int i = 0; i < 6; i++) {
            printf("%c", object1.objectCode[i]);
    }

    printf("\n");

    return 0;
}

我正在尝试将 for 循环(随机生成代码的循环)包装在一个函数中.但是,我尝试这样做的结果只是胡言乱语:

I'm trying to wrap the for loops (the ones randomly generating the code) in a function. However, the print out of my attempt to do this is just gibberish:

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

#define LETTERS_IN_ALPHABET 26

typedef struct object {
    char objectCode[6];
} object_t;


void GetRandomSeed() {
    srand((unsigned)time(NULL));
}

char RandomLetter() {
    return 'A' + rand() % LETTERS_IN_ALPHABET;
}

int RandomDigit() {
    return rand() % 10;
}

void GenerateCode(object_t object) {
    for (int i = 0; i < 2; i++) {
        object.objectCode[i] = RandomLetter();
    }

    for (int i = 2; i < 6; i++) {
        object.objectCode[i] = '0' + RandomDigit();
    }
}

int main() {

    GetRandomSeed();

    object_t object1;

    // Print objectCode string to screen
    for (int i = 0; i < 6; i++) {
            printf("%c", object1.objectCode[i]);
    }

    printf("\n");


    return 0;
}

我在 C 方面有点幼稚,所以我非常感谢您能提供的任何帮助.谢谢.

I'm bit of an infant when it comes to C so I really appreciate any help you can give. Thanks.

推荐答案

您错过了 main 上对 GenerateCode 的调用,这就是打印输出为 胡言乱语.

You're missing the call to GenerateCode on main, that's the reason the print output is gibberish.

然而另一个问题是在 GenerateCode 函数上,参数是按值的,函数不会修改 main 上的原始结构.在 C 中,所有参数都是按值的.你应该传递一个指向结构的指针:

However another issue is that on the GenerateCode function, the parameter is by value and the function won't modify the original struct on main. In C, all and every parameters are by value. You should pass a pointer to the struct:

void GenerateCode(object_t* object) {
    for (int i = 0; i < 2; i++) {
        object->objectCode[i] = RandomLetter();
    }

    for (int i = 2; i < 6; i++) {
        object->objectCode[i] = '0' + RandomDigit();
    }
}

int main() {

    // ...
    object_t object1;

    GenerateCode(&object1);
}

这篇关于C: 生成随机字符串并将其添加到结构体的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆