我有一个incr函数来增加值1
我希望使它成为通用的,因为我不想为相同的功能创建不同的函数.
假设我要增加int,float,char由1
void incr(void *vp)
{
(*vp)++;
}
Run Code Online (Sandbox Code Playgroud)
但我知道的问题是Dereferencing a void pointer is undefined behaviour.有时可能会出错:Invalid use of void expression.
我的功能main是:
int main()
{
int i=5;
float f=5.6f;
char c='a';
incr(&i);
incr(&f);
incr(&c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是如何解决这个问题?是否有解决这个问题的方式C只有
要么
我是否必须incr()为每种数据类型定义?如果是,那么有什么用呢void *
同样的问题swap()和sort().我想交换和整理各种数据类型的功能相同的.
从这个问题延伸出来
我无法理解这段代码.
struct foo myfoo; // --> Is it forward declaration or object creation. ?
struct foo
{
int a;
};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在标记为箭头的代码中-->是前向声明还是对象创建.?
如果那是前向声明那么struct foo;叫什么?如果它是对象创建或实例化,那么它如何在结构定义之前创建对象.
在gcc编译器上它工作正常,但其他编译器给出错误.
gcc -Werror -Wall tst.c -o tst
Run Code Online (Sandbox Code Playgroud)
有关此行为的任何建议或解释gcc?我无法在任何地方找到它.
我在c99标准中找到了这个
3.17.2
1 indeterminate value
either an unspecified value or a trap representation
Run Code Online (Sandbox Code Playgroud)
以上陈述对我来说并不清楚.任何人都可以解释这是什么,它的优点和缺点是什么?
一些例子将受到高度赞赏.
这是我的代码,我想打印15和12,但由于实例成员隐藏a的本地值被打印两次.
#include <stdio.h>
int a = 12;
int main()
{
int a = 15;
printf("Inside a's main local a = : %d\n",a);
printf("In a global a = %d\n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么有没有办法在c中打印它?...顺便说一下,我在c ++中知道它.
while(cond) // fine
for(;cond;) //fine
Run Code Online (Sandbox Code Playgroud)
但当我删除条件部分
while() //syntax compilation error
for(;;) //Infinite loop
Run Code Online (Sandbox Code Playgroud)
这些循环如何在内部实现?或者,编译器(解析器)如何知道空状态while是错误还是for无限?
我没有找到任何关于此的内容,我认为在C中像我这样的人(初学者)可能会有同样的困惑
在Ubuntu上 - 内核2.6.32.2
如何在没有任何库帮助的情况下直接从用户代码调用已有的系统调用?我读书和在互联网上解决这个问题,然后写下面的代码但仍然出错.请帮忙
想要找出当前进程的进程ID
#include <stdio.h>
#include<linux/unistd.h> // for __NR_getpid
_syscall0(int, getpid)
int main() {
printf("Current Process ID : %d\n",getpid());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时出错:
root@Omkant:~/os# gcc -Wall getpid.c -o getpid
getpid.c:5:16: error: expected declaration specifiers or ‘...’ before ‘getpid’
getpid.c:5:1: warning: data definition has no type or storage class
getpid.c:5:1: warning: type defaults to ‘int’ in declaration of ‘_syscall0’
getpid.c: In function ‘main’:
getpid.c:8:2: warning: implicit declaration of function ‘getpid’
Run Code Online (Sandbox Code Playgroud)
代码中的问题是什么?请帮忙...
可能重复,但无法找到相同的.
假设我有以下C代码:
int a;
printf("Enter number :");
scanf("%d",&a); // suppose entered only an integer
// ignoring return value of scanf()
Run Code Online (Sandbox Code Playgroud)
我检查是否一个案例a是zero或non-zero.
if(a)
printf("%d is non-zero",a);
else
printf("%d is zero",a);
Run Code Online (Sandbox Code Playgroud)
一切都很好用if-else,我也知道if-else实现这一目标的其他变化.但问题在于switch-case它说我们可以实现switch-case我们可以做的所有事情if-else.但是以下代码失败了.
switch(a)
{
case a:
printf("%d is non-zero",a);
break;
default:
printf("%d is zero",a);
break;
}
Run Code Online (Sandbox Code Playgroud)
另外我知道在上面的代码中反转这种情况,如下所示将起作用,我会得到答案.
switch(a)
{
case 0:
printf("%d is zero",a);
break;
default :
printf("%d is non-zero",a);
break;
}
Run Code Online (Sandbox Code Playgroud)
但问题是,为什么 …
可能重复:
函数引用
我在一本书中找到了这个代码,
typedef int (&foo)(int,int);
这是什么意思 ?这是对函数的引用吗?如果是,为什么用它?它与指向这样的函数的指针有什么不同.
typedef int (*foo)(int,int);
这只是C++中不是C的增强吗?
编辑:
#include <iostream>
#include <typeinfo>
using namespace std;
int f (int , int ) {return 0;}
int main() {
int (&foo)(int,int) = f;
int (*fp)(int,int) = f;
std::cout<<typeid(foo).name()<<std::endl<<typeid(fp).name();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我打印的类型foo和fp它给了这样的事情:
FiiiE
PFiiiE
什么是E最后的.为什么foo是F同时fp是PF?
我发现一个代码实现为下面显示的类似演示..
struct st
{
int a;
struct
{
int b;
};
};
Run Code Online (Sandbox Code Playgroud)
6.58内未命名的struct/union字段structs/unions
在允许的情况下ISO C11.
但它有什么好处呢?
因为无论如何我可以以相同的方式访问数据成员
int main()
{
struct st s;
s.a=11;
s.b=22;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在gcc 4.5.2上编译,用
gcc -Wall demo.c -o demo
Run Code Online (Sandbox Code Playgroud)
并没有错误,
volatile void * ptr;
Run Code Online (Sandbox Code Playgroud)
无论ptr是volatile或将其指向volatile location.
所以实际的疑问是:同样适用于上述声明,因为它适用于const限定符?
一点点解释都会对我有所帮助.