在scanf空间中,\n是字符i/p的分隔符.以下程序只接受两个输入.我无法理解为什么它接受两个输入.请解释这个行为.
char a,b,c;
scanf("%c%c%c",&a,&b,&c);
printf("%c%c%c",a,b,c);
return 0;
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main()
{
char *arr = "This is to test";
printf("\n%c %c ",*(arr++), *(arr++));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序输出h T而不是输出h h.为什么会这样?
我无法确定以下程序如何输出6和-250.
#include<stdio.h>
int main()
{
unsigned char p=-250;
printf("%d",p);
unsigned int p1=-250;
printf("%d",p1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
作为无符号整数,它必须只输出正值.p值如何输出6?请帮我理解.
有没有办法将绝对地址的整数变量设置0x67a9为值
0xaa55?编译器是纯ANSI编译器.
怎么做到这一点?
这是一个与嵌入式系统相关的程序.在那里我们可以访问特定的内存位置.
我尝试了下面的程序使指针指向一个特定的地址,并在该地址中存储一个值.当我使指针包含指定地址的值时,我得到一个运行时错误,要求我关闭程序.是不是可以为地址0x6778分配一个值.为什么会这样?在什么情况下需要这个?请帮我理解.
int *p=(int*)0x6778;
printf("The address is:%x",p);
Run Code Online (Sandbox Code Playgroud)
当试图做*p = 1000我得到错误.
以下程序在初始化时给出了错误(*a)[5]=((*)[])&v;.当我没有输入该行时,我仍然会收到错误.
int main()
{
int v[10];
int **p;
int (*a)[5];
(*a)[5]=((*)[])&v;
printf("%d\n",*a);
printf("%d\n",sizeof(v));
return 0;
}
Run Code Online (Sandbox Code Playgroud) for (count = index, packet_no = 0;
count < TOTAL_OBJ, packet_no < TOTAL_PKT;
count++, packet_no++)
Run Code Online (Sandbox Code Playgroud)
=> 逗号表达式的左侧操作数无效.
我发现上面的代码是正确的,无法理解为什么会出现这个错误.
#! /usr/bin/perl
use strict;
use warnings;
use File::stat;
my $file_name = 0;
my $info = 0;
my $ret_mode = 0;
my $size;
my $last_mod;
my @array_file;
my $index = 0;
my @array_mode;
my @array_size;
my @array_last_mod;
foreach(@ARGV){
$file_name = $_;
$info = stat($file_name);
$size = $info->size;
$last_mod = scalar(localtime($info->mtime));
$ret_mode = $info->mode;
$ret_mode = $ret_mode & 0777;
$array_file[$index] = ($file_name);
$array_mode[$index] = ($ret_mode);
$array_size[$index] = ($size);
$array_last_mod[$index] = ($last_mod);
$ret_mode = 0;
$index++;
}
my @array_arrays = (@array_file, @array_mode, @array_size, …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
main()
{
int *ptr1 = malloc ( 2 );
int *ptr2 = malloc ( 4 );
int *ptr3 = malloc ( 16 );
printf("ptr1 - %x \n", ptr1);
printf("ptr2 - %x \n", ptr2);
printf("ptr3 - %x \n", ptr3);
*ptr1 = 0x1111;
*ptr2 = 0x2222;
*ptr3 = 0x3333;
#if 1
// silent corruption...
*(ptr1+2) = 0xabcd;
#endif
#if 1
// corruption
*(ptr1+3) = 0xbeee;
#endif
{
int a;
scanf("%d", &a);
}
free(ptr1);
free(ptr2);
free(ptr3);
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,我得到ptr的地址作为ptr1,ptr2,ptr3之间的差值0f 10而不是4个字节的差值.我也在这里检查堆栈损坏.数据段(ptr1,ptr2,ptr3)中的值如何破坏堆栈段(a)中的值.什么是这种沉默的腐败.
我的进程正在访问已创建的共享内存.附加到共享内存的指针是一个包含指针和2或3个变量的结构.
例如:
typedef struct _info_t{
int id;
char c;
}info_t;
typedef struct _details_t{
int buff_id;
info_t* info;
}details_t;
details_t* details = shmat(shmid,(void*)0,0);
printf("\n %d \n",details->info->id); // gives me a segmentation fault
Run Code Online (Sandbox Code Playgroud)