小编Lun*_*nja的帖子

将非空终止字符串传递给printf会导致意外值

这个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 string printf

12
推荐指数
4
解决办法
1831
查看次数

C 中的回调函数与普通函数

我正在学习 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)

c function callback

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

在 haproxy 后端使用域而不是 IP

我有 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)

networking haproxy

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

如何在php中跨线程共享全局变量?

在多线程中,全局变量或资源在线程之间共享.我在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)

php multithreading pthreads

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

指针数组

请帮忙修复这个程序.我试图使用指针而不是数组打印指针数组但我得到了这个错误:

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)

我该如何修复这段代码?请有人解释一下这段代码有什么问题

c

0
推荐指数
1
解决办法
104
查看次数

parameter1.c:4:6:注意:预期'double*'但参数类型为'double'

我有这个问题:

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)

我需要你的帮助来解决这个问题.

c

-1
推荐指数
1
解决办法
3972
查看次数

标签 统计

c ×4

callback ×1

function ×1

haproxy ×1

multithreading ×1

networking ×1

php ×1

printf ×1

pthreads ×1

string ×1