这个C程序给出了一个奇怪的结果:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char str1[5] = "abcde";
char str2[5] = " haha";
printf("%s\n", str1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到:
abcde haha
Run Code Online (Sandbox Code Playgroud)
我只想打印第一个字符串,从代码中可以看出.
为什么要打印它们?
我正在学习 C 中的回调函数,发现自己很难理解回调的概念。
据我所知,c中的回调函数是使用函数指针实现的,这意味着我们可以通过使用指针来引用函数,就像我们使用指针来引用变量一样。
我这里有两个函数的实现:
1.第一种是使用回调函数
#include <stdio.h>
int add_two_number(int a, int b);
int call_func(int (*ptr_func)(int, int), int a, int b);
int main (int *argc, char *argv[])
{
printf("%d", call_func(add_two_number, 5, 9));
return 0;
}
int add_two_number(int a, int b)
{
return a + b;
}
int call_func(int (*ptr_func)(int, int), int a, int b)
{
return ptr_func(a, b);
}
Run Code Online (Sandbox Code Playgroud)
2.第二种是使用普通函数调用:
#include <stdio.h>
int add_two_number(int a, int b);
int call_two_number(int a, int b);
int main (int *argc, char *argv[])
{
printf("%d", …Run Code Online (Sandbox Code Playgroud) 我有 2 个虚拟主机
app.example.com:80 在 IP 地址上 xxx.xxx.xxx.xxx
app2.example.com:80 在 IP 地址上 yyy.yyy.yyy.yyy
我的haproxy ipaddress是 sss.sss.sss.sss
这是 haproxy 配置:
global
log 127.0.0.1 local0 notice
maxconn 2000
user haproxy
group haproxy
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
timeout connect 5000
timeout client 10000
timeout server 10000
frontend www-http
mode http
bind *:80
default_backend appname
stats enable
stats uri /haproxy?stats
stats auth admin:password
stats show-node
backend appname
balance roundrobin
option httpclose
option forwardfor
server lamp1 …Run Code Online (Sandbox Code Playgroud) 在多线程中,全局变量或资源在线程之间共享.我在c中使用pthread库
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *worker(void *);
int ctr = 0;
pthread_mutex_t lock;
int main(int argc, char *argv[])
{
pthread_t t[2];
int i = 0;
//~ pthread_mutex_init(&lock, NULL);
while(i < 2)
{
pthread_create(&t[i], NULL, &worker, NULL);
i++;
}
pthread_join(t[0], NULL);
pthread_join(t[1], NULL);
//~ pthread_mutex_destroy(&lock);
//~ pthread_join(t[1], NULL);
return 0;
}
void *worker(void *arg)
{
//~ pthread_mutex_lock(&lock);
//~ int ctr = 0;
ctr += 1;
printf("job %d started\n", ctr);
sleep(1);
//~ ctr += 1;
printf("job %d finished\n", ctr); …Run Code Online (Sandbox Code Playgroud) 请帮忙修复这个程序.我试图使用指针而不是数组打印指针数组但我得到了这个错误:
pointer_multi_char4.c: In function ‘main’:
pointer_multi_char4.c:7:11: error: expected expression before ‘{’ token
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <stdio.h>
int main (void){
char **message;
message= { "Four", "score", "and", "seven",
"years", "ago,", "our", "forefathers" };
printf("%s\n",message);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我该如何修复这段代码?请有人解释一下这段代码有什么问题
我有这个问题:
parameter1.c: In function ‘main’:
parameter1.c:13:2: error: incompatible type for argument 2 of ‘Countcircumferenceofcircle’
parameter1.c:4:6: note: expected ‘double *’ but argument is of type ‘double’
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <stdio.h>
#define phi 3.14
void Countcircumferenceofcircle(int radius, double *C) {
*C = 2*phi*radius;
}
int main (void) {
int r;
double Circumference;
printf("Insert radius:");
scanf("%d", &r);
Countcircumferenceofcircle(r, Circumference);
printf("Circumference=%f\n", Circumference);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我需要你的帮助来解决这个问题.