小编Abh*_*bey的帖子

无法理解数组的行为

我有以下代码:

 #include<stdio.h>
   void func(int [][3]);

   int main(){
           int a[][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
           func(a);
           printf("%d", a[2][1]);
   }       

  void func(int b[][3]){
          ++b;
          b[1][1] = 17;
  }
Run Code Online (Sandbox Code Playgroud)

问题:
我希望printf语句打印8但是打印17.
我不明白为什么?

谢谢

c arrays

3
推荐指数
2
解决办法
78
查看次数

浏览器按原样显示HTML代码,由C程序编写到套接字

我有以下C程序,该程序以HTML语法将文本写入端口5010。

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 

int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr; 

    char* sendBuff="<html><head><title>page 1</title></head></html>";
    time_t ticks; 
    uint32_t ip = 0;
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    //memset(sendBuff, '0', sizeof(sendBuff)); 
    inet_aton("127.0.0.1", (struct in_addr*)&ip);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(ip);
    serv_addr.sin_port = htons(5010); 

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    listen(listenfd, 10); 

    while(1)
    {
        connfd = accept(listenfd, …
Run Code Online (Sandbox Code Playgroud)

html c sockets webserver http

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

C中的命令行参数,无法理解其行为

我有以下代码:

#include <stdio.h>
int main(int argc, char* argv[]){
    int a = argv[1]?atoi(argv[1]):10;
    int b = argv[2]?atoi(argv[2]):20;
    printf("a = %d, b = %d\n", a, b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我不提供任何命令行输入,则"a"和"b"中的值
应分别为10和20,但会发生的是"a"获取值为10而"b"为0.
我无法理解为什么这种情况正在发生,因为我
在两种情况下做的都是一样的.

谢谢.

c command-line

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

无法理解C程序中"系统"函数调用的行为

当我运行以下程序时,输出system("ls -l")显示在之前printf.为什么会这样?

#include<stdio.h>  
int main()  
{  
    printf("\nHello world");  
    system("ls -l"); // output of this statement is displayed before that of the preceding 
                     // printf statement
    return 0;  
}  
Run Code Online (Sandbox Code Playgroud)

谢谢.

c system call

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

标签 统计

c ×4

arrays ×1

call ×1

command-line ×1

html ×1

http ×1

sockets ×1

system ×1

webserver ×1