相关疑难解决方法(0)

131
推荐指数
4
解决办法
12万
查看次数

随机字符串生成

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

char *charStr;
int stringLength;

void genRandom() {
    static const char alphanum[] =
        "0123456789"
        "!@#$%^&*"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < stringLength; ++i) {
        charStr[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    charStr[stringLength] = 0;
}

int main()
{
    while(true)
    {
        genRandom();
        cout < charStr;
    }
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

编译时会出现问题.它会编译得很好但没有任何显示,然后程序将停止运行.所以我的问题是,这段代码出了什么问题?

c++ string random char

5
推荐指数
1
解决办法
2万
查看次数

例如,为什么我们只是初始化3个中的1个成员时,为什么结构的所有元素都初始化为0

例如:

typedef struct student { 

     int rollno;
     float cgpa;
     char name[20];

}Student;

Student me= {0,0}; // will intilize name with all zeros
Run Code Online (Sandbox Code Playgroud)

c structure

2
推荐指数
1
解决办法
127
查看次数

结构变量是否默认初始化?

我没有在网上找到任何关于在 C 中创建对象时会发生什么的信息:比如它们的值被初始化或它们采用垃圾值。

#include <stdio.h> 
struct temp 
{ 
    int a; 
} s;

int main() 
{ 
    printf("%d", s.a); 
}
Run Code Online (Sandbox Code Playgroud)

输出是:0

那么 0 是垃圾值吗??或者这是一个未定义的行为?

c struct

1
推荐指数
1
解决办法
75
查看次数

标签 统计

c ×3

c++ ×1

char ×1

declaration ×1

initialization ×1

random ×1

string ×1

struct ×1

structure ×1