我有以下 udp 服务器:
/************* UDP SERVER CODE *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main(){
int udpSocket, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size, client_addr_size;
int i;
/*Create UDP socket*/
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(20001);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*Bind socket with address struct*/
bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*Initialize size variable to be used …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
enum e {
X=1,
Y,
Z
}
Run Code Online (Sandbox Code Playgroud)
如果将第一个元素定义为1,那么枚举元素的值是多少?
其他元素每个会增加1吗?
我想定义一个同时支持的宏函数:
1)没有输入参数
2)输入参数
有点像:
#define MACRO_TEST(X)\
printf("this is a test\n");\
printf("%d\n",x) // the last printf should not executed if there is no input parameter when calling the macro
Run Code Online (Sandbox Code Playgroud)
主要:
int main()
{
MACRO_TEST(); // This should display only the first printf in the macro
MACRO_TEST(5); // This should display both printf in the macro
}
Run Code Online (Sandbox Code Playgroud) 是否可以定义一个函数指针数组(并且这些函数没有相同的输入参数),如下面的代码所示?
如果是,我必须在函数定义中输入什么 int (*handler)(/*what Ihave to put here ?*/);
struct handler_index {
const char *name;
int (*handler)(/*what Ihave to put here ?*/);
};
int handler0 (int a, int b)
{
printf("%d\n",a+b);
}
int handler1 (int a, int b, int c)
{
printf("%d\n",a+b+c);
}
int handler2 (int a, int b, int c, int d)
{
printf("%d\n",a+b+c+d);
}
const struct handler_index handler_index[] = {
[0] = {"handler0", handler0},
[1] = {"handler1", handler1},
[2] = {"handler2", handler3},
};
Run Code Online (Sandbox Code Playgroud) 在automake
我想为Makefile变量分配一个命令执行结果
如何在configure.ac中执行此操作?
假设我有一些代码执行此操作:
while(1) {
scanf("%c", &key);
// if E or e, exit
if (key == 'E' || key == 'e')
break;
}
Run Code Online (Sandbox Code Playgroud)
这显然不会占用甚至单个核心的所有资源.它只是......"坐在那里",直到有人真正按下E.我的问题是:运行时如何发现它不应占用核心的所有资源等待scanf?scanf是一个特殊情况,因为它是I/O,因此操作系统安排它直到有效按下一个键?我可以以某种方式强迫它一直在while说,a++在循环中添加一个内部吗?
我在 bash 中有以下字符串
str="kallel"
Run Code Online (Sandbox Code Playgroud)
我想str从str2. 包含str2重复str直到长度 = 20。所以结果应该是这样的:
str2="kallelkallelkallelka"
Run Code Online (Sandbox Code Playgroud)
在 bash 中怎么做?
如何将函数名称显示到函数中?
#!/bin/sh
toto()
{
echo "$something"
}
toto
Run Code Online (Sandbox Code Playgroud)
上面的代码应该显示
toto
Run Code Online (Sandbox Code Playgroud)
我在busybox中使用bash linux
我在shell脚本中多次看到他们"_"在if比较中使用,如下所示:
if [ "_$str" = "_" ]; then ....; fi
Run Code Online (Sandbox Code Playgroud)
上面的代码检查str变量是否为空,并进行比较if [ "_$str" = "_" ].
为什么不用if [ "$str" = "" ]?为什么要加"_"两个字符串?
我想在c代码中编写宏来释放许多像这样的指针:
FREE(ptr1, ptr2, ptr3, ptr4, ptrx);
Run Code Online (Sandbox Code Playgroud)
对我来说,这比
FREE(ptr1);
FREE(ptr2);
FREE(ptr3);
FREE(ptr4);
FREE(ptrx);
Run Code Online (Sandbox Code Playgroud)
提前致谢
问候,