小编The*_*nic的帖子

在C中转换指针的规则是什么?

K&R不会过去,但他们会使用它.我尝试通过编写示例程序来了解它是如何工作的,但它并没有那么顺利:

#include <stdio.h> 
int bleh (int *); 

int main(){
    char c = '5'; 
    char *d = &c;

    bleh((int *)d); 
    return 0;  
}

int bleh(int *n){
    printf("%d bleh\n", *n); 
    return *n; 
}
Run Code Online (Sandbox Code Playgroud)

它编译,但我的print语句吐出垃圾变量(每次调用程序时它们都不同).有任何想法吗?

c pointers casting

60
推荐指数
5
解决办法
13万
查看次数

C中的模运算有哪些规则?

在早期的课程中,我被教导n % d = r并将其视为n = d*q + r,d除数在哪里,q是商,并且r是余数(注意其余部分永远不会是负数).

因此,例如,-111 mod 1110,因为-111 = -11*-11 + 10(相反-111 = -11*10 -1,看到这将如何给我们一个负余数).

但是,打印结果时-111 % 11,-1结果并非如此10.为什么?这在技术上是不是错了?

c modulus

21
推荐指数
2
解决办法
8838
查看次数

灵活的阵列成员真的有必要吗?

显然,具有灵活数组成员的结构不是要声明的,而是与指向该结构的指针一起使用.声明灵活的数组成员时,必须至少有一个其他成员,并且灵活数组成员必须是该结构中的最后一个成员.

假设我有一个看起来像这样的:

struct example{
    int n;
    int flm[]; 
}
Run Code Online (Sandbox Code Playgroud)

然后要使用它,我将必须声明一个指针并使用malloc为结构的内容保留内存.

struct example *ptr = malloc(sizeof(struct example) + 5*sizeof(int)); 
Run Code Online (Sandbox Code Playgroud)

也就是说,如果我希望我的flm []数组保持五个整数.然后,我可以像这样使用我的结构:

ptr->flm[0] = 1; 
Run Code Online (Sandbox Code Playgroud)

我的问题是,我不应该只使用指针而不是这个吗?它不仅在C99之前兼容,而且我可以在有或没有指向该结构的指针的情况下使用它.考虑到我已经必须将malloc与flm一起使用,我不应该只能这样做吗?

考虑示例结构的这个新定义;

struct example{
    int n; 
    int *notflm; 
}

struct example test = {4, malloc(sizeof(int) * 5)}; 
Run Code Online (Sandbox Code Playgroud)

我甚至可以像柔性阵列成员一样使用替换:

这也有用吗?(如上面的示例定义为notflm)

struct example test; 
test.n = 4; 
notflm = malloc(sizeof(int) * 5); 
Run Code Online (Sandbox Code Playgroud)

c struct flexible-array-member

12
推荐指数
1
解决办法
1682
查看次数

变量如何在递归中存储在内存中?

我不确定在递归过程中如何创建变量并将其存储在内存中.以下是取自C Primer Plus的示例:

#include <stdio.h>
void recursiontest(int); 

int main(){
    recursiontest(3); 
    return 0; 
}


void recursiontest(int n){
    printf("Level %d : %#x\n", n, &n); 
    if(n < 4)
        recursiontest(n + 1); 
    printf("LEVEL %d : %#x\n", n, &n);
    return;  
}
Run Code Online (Sandbox Code Playgroud)

产生输出:

第3级:0x3ce1f8bc

第4级:0x3ce1f89c

等级4:0x3ce1f89c

第3级:0x3ce1f8bc

似乎原始函数调用的本地"n"变量的地址顺序晚于第一个(也是唯一的)递归调用的地址.这是为什么?

当我调用一个函数时,是不是根据传递给它的实际参数声明和定义了它的形式参数?这是不是意味着第一次调用的本地整数n是在第二次(递归)调用之前创建的?第二次通话的n怎么可能比第一次通话更早?

c recursion function

8
推荐指数
1
解决办法
2492
查看次数

这个O(n ^ 4)算法可以避免吗?

免责声明:这不是一个家庭作业问题.我甚至不上学.

#include <stdio.h>
void printIntersect(float, float, float, float);

int main(){
   int x, y, z, a;
   float slopes[] = {5, 9, 4/3.0f, 2/3.0f, 3};
   float inter[] = {3, 2, 1, 0, 5/3.0f};  

   for(x = 0; x < (sizeof(slopes) / sizeof(slopes[0])) - 1; x++)
       for(y = 1; y < (sizeof(slopes) / sizeof(slopes[0])); y++)
           for(z = 0; z < sizeof(inter) / sizeof(inter[0]); z++)
                  for(a = 0; a < sizeof(inter) / sizeof(inter[0]); a++)
                      if(slopes[x] != slopes[y])
                          printIntersect(slopes[x], slopes[y], inter[z], inter[a]);

   return 0;
}

void …
Run Code Online (Sandbox Code Playgroud)

c arrays

6
推荐指数
1
解决办法
199
查看次数

C90中是否真的不允许使用可变长度数组?

我正在阅读C Primer Plus中的VLA,本书严格地说,将C语言引入到C的标准开始.每当我尝试在for循环的标头内声明循环控制变量时,gcc会通知我此操作仅在C99模式下允许.但是,以下测试代码编译并工作(虽然它打印垃圾变量,考虑到没有初始化的数组元素,这是预期的).

#include <stdio.h>

int main(){
    int x; 
    int i = 9; 
    int array[i]; 

    for(x = 0; x < i; x++)
        printf("%d\n", array[x]);

    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

如果我不是C99模式,这怎么可能合法?

c arrays gcc gcc-extensions

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

qsort() 给出随机结果

在上面的程序中,我使用 malloc 创建了一个指向 char 的指针数组,然后尝试使用 qsort 对这些“字符串”进行排序。我得到了不正确的结果。更重要的是,每次运行程序时,我都会得到不同的结果。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 1000 
#define MAXCHARS 1000 
int ballin_compare(const void *, const void *); 

int main(int argc, char *argv[]){
    char *linebuffer, **pointbuffer; 
    FILE *fp;
        int i = 0;  

        if(argc < 2 || (fp = fopen(argv[1], "r")) == NULL)
            return 1; 

    linebuffer = (char *)malloc(MAXCHARS); 
    pointbuffer = (char **)malloc(sizeof(char *) * MAXLINE); 

    while(i < MAXLINE && fgets(linebuffer, MAXCHARS, fp) != NULL){
        pointbuffer[i] = (char *)malloc(strlen(linebuffer)); 
        strcpy(pointbuffer[i++], linebuffer); 
    }

    free(linebuffer); …
Run Code Online (Sandbox Code Playgroud)

c sorting

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

如何在我的程序中正确使用fread()和fwrite()?

/*Write a program that indefinitely takes in files to append to a master
  file. If the file names are the same, don't do it. If there's an error,
  don't do it. Exit with a blank line. Use a custom buffer.*/ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 25
#define BUFSIZE 1024
int append(FILE *, FILE *); 

int main(){
    char src[MAX_NAME], mainfile[MAX_NAME]; 
    FILE *src_ptr, *mainfile_ptr; 
    int successes = 0; 

    puts("Enter name of main file"); 
    if(gets(mainfile) == NULL){
        fprintf(stderr, "Error, couldn't …
Run Code Online (Sandbox Code Playgroud)

c io file fwrite fread

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

for循环中的指令只执行一次?

我的程序中有一个for循环.它看起来就像这样

#include <stdio.h>
int main(){
    int n = 0;
    int i;
    for(i = 0; i < 100; i++){
        n = i*2;
        if(n >= 50)
            n-=50;

        printf("n is %d\n", n); 
    }
    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

该计划只是在它达到50后n重置为0.这就是全部,但是当我打印出来时,n只会被重置一次.是什么赋予了?

c loops

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