小编use*_*997的帖子

使用scanf读取unsigned char

我正在尝试使用此代码读取0到255(unsigned char)之间的值.

#include<stdio.h>
int main(void)
{
    unsigned char value;

    /* To read the numbers between 0 to 255 */
    printf("Please enter a number between 0 and 255 \n");
    scanf("%u",&value);
    printf("The value is %u \n",value);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我按预期得到了以下编译器警告.

warning: format ‘%u’ expects type ‘unsigned int *’, but argument 2 has type ‘unsigned char *’

这是我对该计划的输出.

Please enter a number between 0 and 255
45
The value is 45 
Segmentation fault

运行此代码时,我确实遇到了分段错误.

unsigned char使用读取值的最佳方法是什么scanf

c scanf

20
推荐指数
1
解决办法
3万
查看次数

9
推荐指数
3
解决办法
3万
查看次数

如何将shell变量导出到所有会话?

我想知道有没有办法将我的shell变量导出到系统中的所有会话(不仅是当前会话).我不打算在.bashrc文件中设置它,因为shell变量是一个动态的变量,它不时变化.

bash shell

6
推荐指数
3
解决办法
5968
查看次数

将指针分配给整数

我可以将指针分配给整数变量吗?如下所示。

int *pointer;
int array1[25];
int addressOfArray;

pointer = &array1[0];

addressOfArray = pointer;
Run Code Online (Sandbox Code Playgroud)

有可能这样做吗?

c pointers assignment-operator

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

期望脚本中的通配符不起作用

我有以下脚本成功运行。但是,如果我尝试使用通配符复制多个文件,则会引发错误,说“没有这样的文件或目录”。

此代码有效:

#!/usr/bin/expect -f
spawn scp file1.txt root@192.168.1.156:/temp1/.
expect "password:"
send "iamroot\r"
expect "*\r"
expect "\r"
Run Code Online (Sandbox Code Playgroud)

以下不起作用:

#!/usr/bin/expect -f
spawn scp * root@192.168.1.156:/temp/. #fails here
….
Run Code Online (Sandbox Code Playgroud)

testing expect

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

C中循环缓冲区中的反向遍历

我有以下数组,我打算将其用作循环缓冲区。

int array1[20]; 
Run Code Online (Sandbox Code Playgroud)

该数组由一个线程写入并由另一个线程读取。在阅读时,我需要读取写入该数组的最后 3 个值。

写作工作正常。我用

writeIndex = (writeIndex + 1) %20;
Run Code Online (Sandbox Code Playgroud)

来写。这很好地将数组索引 0 滚动到 19。

为了阅读,我正在使用

readIndex = (readIndex -1)%20;
Run Code Online (Sandbox Code Playgroud)

但它不起作用当我尝试从索引 0 到 19 时。

c linux

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

如何处理GNU中的子make使错误?

我在Makefile(GNU)中使用sub-make.但是,只要sub-make失败,主make就会继续成功运行.每当我的子make失败时,我希望我的主Makefile失败.我怎么做?

all:   
    pushd ${STA_DIR};make clean;make ARGS=${A1},${A2};popd
Run Code Online (Sandbox Code Playgroud)

每当STA_DIR中的make失败时,主make就不会停止.我想主要制作停止.

makefile gnu-make

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

标签 统计

c ×3

bash ×2

assignment-operator ×1

expect ×1

gnu-make ×1

linux ×1

makefile ×1

pointers ×1

scanf ×1

shell ×1

testing ×1