小编bab*_*bon的帖子

部分初始化C结构

链接指出"当自动数组或结构具有部分初始化程序时,其余部分初始化为0".我决定尝试我读到的内容,并编写以下代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    //int arr[3] = {2};  // line no. 7

    struct s {
        int si;
        int sj;
    };

    struct s myStruct;
    myStruct.si = 9;
    printf("%d\n", myStruct.sj);
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么4096(我相信这是一些"垃圾"价值)在我评论时打印出来line no. 7,0当我取消评论时我得到了line no. 7.我不认为arr声明与main()激活记录(或更确切myStruct)有关,应该是这样(如果我们没有line no. 7注释):

---------------
|  Saved PC   |
---------------
|  arr[2]     |
---------------
|  arr[1]     |
---------------
|  arr[0]     |
---------------
|  si …
Run Code Online (Sandbox Code Playgroud)

c struct initialization variable-assignment

27
推荐指数
3
解决办法
9496
查看次数

关于Haskell类型类(Num vs Read)

有人可以解释我在这里缺少的东西:

Prelude> :t read
read :: (Read a) => String -> a 
Prelude> read "4"

<interactive>:1:0:
    Ambiguous type variable `a' in the constraint:
      `Read a' arising from a use of `read' at <interactive>:1:0-7
    Probable fix: add a type signature that fixes these type variable(s)
Run Code Online (Sandbox Code Playgroud)

read "4"引发错误,因为ghci不知道我们想要哪种具体类型,它只知道我们有一个Read类型类.read "4" :: Int工作良好.这对我来说很清楚.

现在,按照上面的逻辑,我希望fromIntegral 4引发一个错误:

Prelude> :t fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b
Prelude> fromIntegral 4
4
Run Code Online (Sandbox Code Playgroud)

但是,它工作正常.为什么在这种情况下不需要类型注释?我预计上述情况会失败; 而且只有

Prelude> fromIntegral 4 …
Run Code Online (Sandbox Code Playgroud)

haskell

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

递增运算符和"未定义的行为"

comp.lang.c FAQ中所述,C标准规定:

在前一个和下一个序列点之间,对象的存储值最多只能通过表达式的计算来修改一次.此外,只能访问先前值以确定要存储的值.

但是,这篇(现已删除的)文章说这a = ++b + ++c;是未定义的.有人可以解释为什么这是未定义的行为?

c language-lawyer

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

块作用域变量和激活记录

我知道,局部变量(以及其他内容)被放置在激活记录中。并且在函数开始执行之前,激活记录必须存在。考虑以下功能:

void f(void)
{
    int i;
    scanf("%d", &i);
    if (i > 10) {
        int j = 22;
        // do some operations on j here.
    }
    // more code below...
}
Run Code Online (Sandbox Code Playgroud)

查看此函数,似乎该变量j可能存在或可能不存在,完全取决于运行时用户的输入。在这种情况下,

  1. 变量j将被放置在激活记录中吗?
  2. 是否定义了此实现(换句话说,某些编译器会生成等同于j在if`块之外和之上声明的代码)吗?
  3. 或者,j如果需要,将在执行期间将其简单地分配到堆栈段上吗?但是,在这种情况下,如何jif块之后超出范围?

我在C11规范中找不到关于此的太多信息。提前致谢。

c activation-record

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

关于 errno 和 strerror()

考虑到fopen()失败,在下面这段代码中:

FILE *fp = fopen("file.txt", "w");
if (fp == NULL) {
    printf("Error occurred while opening file, errno=%d, %s\n", 
                         errno, strerror(errno));
    exit(1);
}
Run Code Online (Sandbox Code Playgroud)

由于在 C 中未指定函数参数求值的顺序,因此在调用 时printf(),如果strerror()首先求值(调用)调用并且失败,那么当实际打印该行时,不会errno 将其设置为其他内容吗?或者,它是否会在评估之前就errno被复制到激活记录中,因此保持不变?这是未指定的行为吗?printf()strerror()

编辑: 是的,我确实明白我可以在之后立即errno保存,但这不是我的观点。我试图弄清楚上面这段代码的行为方式。intfopen()

c evaluation arguments errno

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

关于char的指针数组

我理解为什么这不起作用:

int main(int argc, char *argv[]) {
    char *names[] = {"name1", "name2", "name3", "name4"};
    int i = 0;
    while (i++ <= 3) {
        printf("%s\n", *names++);
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

a.c: In function 'main':
a.c:16: error: wrong type argument to increment
shell returned 1
Run Code Online (Sandbox Code Playgroud)

这是因为我试图增加一个数组变量(而不是一个指针).请不要介意错误消息中的行号,我在上面和下面列出了很多注释代码.

但是,我不明白为什么这段代码有效:

void myfunc(char *names[]) {
    int i = 0;
    while (i++ <= 3) {
        printf("%s\n", *names++);
    }
}


int main(int argc, char *argv[]) {
    char *names[] = {"name1", "name2", "name3", "name4"};
    myfunc(names);
}
Run Code Online (Sandbox Code Playgroud)

我们怎样才能增加namesmyfunc() …

c arrays pointers

3
推荐指数
1
解决办法
66
查看次数

激活记录 - C.

请考虑以下计划:

#include <stdio.h>

void my_f(int);

int main()
{
    int i = 15;
    my_f(i);
}

void my_f(int i)
{
    int j[2] = {99, 100};
    printf("%d\n", j[-2]);
}
Run Code Online (Sandbox Code Playgroud)

我的理解是,活动记录(又名堆栈帧)my_f()应该是这样的:

    ------------
    |     i    |    15
    ------------
    | Saved PC |    Address of next instruction in caller function
    ------------
    |   j[0]   |    99
    ------------
    |   j[1]   |    100
    ------------
Run Code Online (Sandbox Code Playgroud)

我期望j [-2]打印15,但它打印0.有人请解释我在这里缺少什么?我在OS X 10.5.8上使用GCC 4.0.1(是的,我生活在一块岩石下,但除了这里的重点之外).

macos x86 assembly gcc abi

3
推荐指数
1
解决办法
359
查看次数

初始化struct的成员

我正在阅读structC语言中的s,我并不完全理解a struct初始化的方式.请考虑以下代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct tnode {
    char *word;
    int count;
    struct tnode *left;
    struct tnode *right;
};


int main (void)
{
    struct tnode *root;
    struct tnode tn1;
    root = &tn1;
    printf("%d\n", root->count);

    if (root->word == NULL)
        printf("word is NULL\n");
    else
        printf("word is not NULL\n");

    if (root->right == NULL)
        printf("rightis NULL\n");
    else
        printf("right is not NULL\n");
}
Run Code Online (Sandbox Code Playgroud)

输出:

0
word is NULL
right is not NULL
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么root->right没有初始化NULL.有人可以请一些光吗?

c struct initialization

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

打印矩阵

问题是:给定第一行画布的高度(h)和宽度(w)(换句话说,2D数组); 和圆的中心(x,y)和半径(r)的坐标,打印画布.如果2D阵列的元素在圆圈内,则打印#否则打印..以下是我的尝试,但对于我的生活,我无法弄清楚为什么2D矩阵只包含.s.请稍微说清楚:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

typedef struct point {
    int x;
    int y;
} point;

float distance(point p, point q) {
    return sqrt((p.x - q.x)*(p.x - q.x) + (p.y - q.y)*(p.y - q.y));
}

int withinCircle(point circleCentre, point p, int radius) {
    float dist = distance(circleCentre, p);
    if (!(dist > (float) radius)) {
        return 1;
    } else {
        return 0;
    }
}

int …
Run Code Online (Sandbox Code Playgroud)

c algorithm multidimensional-array

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

关于Scheme中的引用(')

我的理解是,'Scheme 中的单引号用于告诉Scheme,后面的内容是符号而不是变量.因此,不应该评估它.基于这种理解,1.0当我进入'3/3REPL 时,我不明白为什么鸡打印.

CHICKEN
(c) 2008-2016, The CHICKEN Team
(c) 2000-2007, Felix L. Winkelmann
Version 4.11.0
linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
compiled 2016-08-23 on buildvm-13.phx2.fedoraproject.org

#;1> '3/3
1.0
Run Code Online (Sandbox Code Playgroud)

我希望它能打印出来3/3.为什么要对此进行评估而不是报价?谢谢.

lisp scheme chicken-scheme

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

指针,后增量运算符和序列点

当我运行这个玩具程序时,

#include<stdio.h>

int main(void)
{
    int arr[] = {11, 22, 33};
    int *ptr = arr;
    printf("ptr at: %p\n", (void *)ptr);
    printf("Result of expression: %d\n", *ptr++);  // line 8
    printf("ptr at: %p\n", (void *)ptr);
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

ptr at: 0x7fffffffdbf0
Result of expression: 11
ptr at: 0x7fffffffdbf4
Run Code Online (Sandbox Code Playgroud)

我对发生的事情的理解line 8是,根据优先规则(后增量的++优先级高于取消引用*),表达式*ptr++相当于*(ptr++).是我经历的一个很好的解释.但是,在那个解释中没有提到序列点.

在混合中抛出序列点,我假设它11是打印的,因为ptr只有在达到序列点(恰好所有要printf计算的参数之后)才会增加.并且,后增量运算符实际上是在序列点之后完成它(增加和写入)的工作.我的理解是否正确?

c pointers

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