【发布时间】:2014-11-13 04:36:28
【问题描述】:
试图将 char 数组作为函数的参数传递,但它没有被传递。具体来说,尝试将 unsigned char 数组“p”传递给函数“setlsbs”(忽略函数的明显目的。目前只是尝试正确传递它)。
代码:
#include <stdio.h>
#include <stdlib.h>
#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(byte) \
(byte & 0x80 ? 1 : 0), \
(byte & 0x40 ? 1 : 0), \
(byte & 0x20 ? 1 : 0), \
(byte & 0x10 ? 1 : 0), \
(byte & 0x08 ? 1 : 0), \
(byte & 0x04 ? 1 : 0), \
(byte & 0x02 ? 1 : 0), \
(byte & 0x01 ? 1 : 0)
#define PRINTBIN(x) printf(BYTETOBINARYPATTERN, BYTETOBINARY(x));
void setlsbs(unsigned char* p, unsigned char b0);
unsigned char getlsbs(unsigned char *p);
//MAIN
int main(int argc, char **argv){
//default seed
long seed = 1234;
//if argv[1] available, use it as the seed instead
if(argv[1]){
sscanf(argv[1], "%ld", &seed);
}
//seed RNG
srand(seed);
//make array for eight bytes
unsigned char *p[8];
//fill array with random num 0-255
int cnt;
for(cnt = 0; cnt<8; cnt++){
p[cnt] = (unsigned char*)(rand()%255);
printf("p[%d] decimal:%d and binary:", cnt, p[cnt]);
PRINTBIN((int)p[cnt]);
printf("\n");
}
//make random num for b0
unsigned char b0 = (unsigned char)(rand()%255);
printf("b0 decimal:%d and binary:", b0);
PRINTBIN((int)b0);
printf("\n");
//call setlsbs
setlsbs((unsigned char*)p, (unsigned char)b0);
}
//SET LSBS
void setlsbs(unsigned char *p, unsigned char b0){
printf("p[0]: %d\n", p[0]);
}
//GET LSBS
unsigned char getlsbs(unsigned char *p){
}
结果:
p[0] decimal:243 and binary:11110011
p[1] decimal:175 and binary:10101111
p[2] decimal:32 and binary:00100000
p[3] decimal:230 and binary:11100110
p[4] decimal:117 and binary:01110101
p[5] decimal:189 and binary:10111101
p[6] decimal:29 and binary:00011101
p[7] decimal:227 and binary:11100011
b0 decimal:233 and binary:11101001
p[0]: 0
最后一行应该是 p[0]: 243
谢谢!
【问题讨论】:
-
//make array for eight bytes-unsigned char *p[8]不。那是八个 指针 (尽管它的大小肯定超过 8 个八位字节)。 -
你有没有停下来思考一下为什么这个演员阵容是必要的 -
(unsigned char*)(rand()%255)?在编译器关闭之前,转换不是你扔给编译器的东西。 -
@WhozCraig - 谢谢。固定。
-
@Praetorian - 谢谢。我仍然在学习很多关于 C 的知识。我已经改变了这一点,并将在未来牢记这一点。谢谢。
-
这一行:unsigned char *p[8];创建一个由 8 个指针组成的数组,其中的指针没有它们所指向的具体事物。
标签: c arrays function pointers parameters