小编klu*_*utt的帖子

typedef和变量名

忽略我为什么要这样做,只是试图理解这里发生了什么:这段代码编译:

#include <stdio.h>
typedef char byte;

int main (void) 
{
    byte var_byte;
    int byte = 10;

    printf("\n Test program: %d\n", byte);
}  
Run Code Online (Sandbox Code Playgroud)

但是,如果我改变声明变量的顺序,它就不会编译.

这不编译:

#include <stdio.h>
typedef char byte;

int main (void)
{
    int byte = 10;
    byte var_byte;

    printf("\n Test program: %d\n", byte);
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

b.c:7:8: error: expected ‘;’ before ‘var_byte’
   byte var_byte;
        ^~~~~~~~
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么订单很重要吗?

c variables scope typedef

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

Python使用特定的文件名下载youtube

我正在尝试通过pytube以下方式下载YouTube视频:

from pytube import YouTube
YouTube('http://youtube.com/watch?v=9bZkp7q19f0').streams.first().download()
Run Code Online (Sandbox Code Playgroud)

但该文件将具有与原始视频名称相同的名称。如何指定自定义文件名?

python youtube pytube

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

如果变量离开作用域,为什么C不会递减堆栈指针?

#include <stdio.h>
void main() {
    {
        int x;
        printf("%p\n", &x);
    }
    {
        int x;
        printf("%p\n", &x);
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为运行此命令将输出相同的内容两次。当它声明第一个变量时,它会递增堆栈指针,但会离开作用域,因此会递减它,然后第二次重复该过程,因此int x两次都将占用堆栈上的相同内存位置。

但是事实并非如此。堆栈指针不会递减,int x在两种情况下都将占用堆栈中的不同位置。实际上,int x即使范围已消失,第一个仍然可以访问。

#include <stdio.h>
void main() {
    {
        int x = 10;
        printf("%p\n", &x);
    }
    {
        int x = 25;
        printf("%p\n", &x);
    }
    {
        int x = 71;
        printf("%p\n", &x);

        int *p = &x;
        printf("%i %i %i\n", *(p + 2), *(p + 1), *p);
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么是这样?我有什么误会?

c stack-pointer

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

参数如何具有类型但没有名称?

我看到一个被标记为欺骗的问题,但是该问题的一部分没有得到欺骗的回答,并且我没有找到合适的欺骗来纠正它。所以去。

我曾经看到这样的声明:

int (*function)(int, float);
Run Code Online (Sandbox Code Playgroud)

我不太了解 它有两个参数,但没有名称。这是如何运作的?我的意思是,在声明这样的函数时:

int f(int x, int y) {
    return x+y;
}
Run Code Online (Sandbox Code Playgroud)

没有标识符怎么办?我注意到这行不通,甚至在第一行给出了一个编译器错误,说

int f(int, int) {
    return /* What should I even write here? */ ;
}
Run Code Online (Sandbox Code Playgroud)

我得到两个错误:

f.c:1:7: error: parameter name omitted
 int f(int, int)
       ^~~
f.c:1:7: error: parameter name omitted
 int f(int, int)
            ^~~
Run Code Online (Sandbox Code Playgroud)

c function-pointers function-prototypes

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

Shift + Enter 与在控制台中输入

我正在尝试捕获通过stdin流输入的所有字符(EOF 除外)。我想输入一个多行文本:每行\n末尾。

int getline(char s[])
{
    printf("Call-getline()\n");

    int c;
    int idx=0;

    while((c=getchar()) != EOF)
    {
        s[idx++] = c;
    }

    printf("\n-EOF found--\n");

    s[idx] = '\0';

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

我不知道如何摆脱\n按 Enter 时得到的那个,我想知道shif+entervs 是否enter alone有什么不同。我阅读了它在 Microsoft Word 中的作用:新段落与换行符。

c enter shift

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

int_fast8_t 和 int_fast16_t 的区别

我是嵌入式系统的初学者,我在头文件中看到int_fast8_t/int_fast16_t/int_fast32_t都是带符号的 int。那么它们之间有什么区别吗?或者它们基本相同,即使您为 int_fast8_t 分配了 32 位,它仍然会存储 32 位而不是 8 位?

    /* fastest minimum-width signed integer types */
typedef    signed            int    int_fast8_t;
typedef    signed            int    int_fast16_t;
typedef    signed            int    int_fast32_t;
typedef    signed        __INT64    int_fast64_t;
Run Code Online (Sandbox Code Playgroud)

c embedded stm32

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

铸造真的有什么作用吗?

考虑以下片段:

char x[100];
double *p = &x;
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,这会产生以下警告:

f.c:3:15: warning: initialization of ‘double *’ from incompatible pointer type ‘char (*)[100]’ 
[-Wincompatible-pointer-types]
    3 |   double *p = &x;
      |               ^
Run Code Online (Sandbox Code Playgroud)

这很容易解决,只需更改为

double *p = (double*)&x;
Run Code Online (Sandbox Code Playgroud)

我的问题是,演员阵容真的有什么作用吗?如果没有演员表,代码会无效吗?或者这只是一种让编译器安静的方法?什么时候需要铸造?

我知道你可以对这样的片段产生一些影响:

int x = 666;
int y = (char)x;
Run Code Online (Sandbox Code Playgroud)

但这不和这个一样吗?

int x = 666;
char c = x;
int y = c;
Run Code Online (Sandbox Code Playgroud)

如果是相同的,那么演员表会一些事情,但没有必要。对?

请帮助我理解这一点。

c casting

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

传递给函数时,如何强制警告使用错误大小的数组?

假设您有一个将字符串作为参数的函数:

void foo(char *arg);
Run Code Online (Sandbox Code Playgroud)

如果我们确定数组(不要与字符串长度混淆,谢谢chux)总是有一定的大小,比方说 8,那么我们可以改为:

void bar(char (*arg)[8]);
Run Code Online (Sandbox Code Playgroud)

然后像这样调用它:

char str[8] = "Hello";
bar(&str);
Run Code Online (Sandbox Code Playgroud)

我们需要添加&它才能正常工作,但是如果您传递大小或类型错误的数组,上面的代码将发出警告,这正是我想要实现的。但是我们显然需要稍微修改一下身体。所以我的问题很简单,这种包装技术是否可行:

void bar(char (*arg)[8]) {
    char *tmp = (char*) arg;
    foo(tmp);
}
Run Code Online (Sandbox Code Playgroud)

我在这里试图实现的是,如果使用大小错误的数组调用,则应该发出警告。上述解决方案安全吗?将指向char数组的指针转换为指向char的指针是否安全?我试过了,它工作正常,并且没有发出警告-Wall -Wextra -pedantic。一旦我改变了我的大小,str我就会得到:

<source>: In function 'main':
<source>:18:9: warning: passing argument 1 of 'bar' from incompatible pointer type [-Wincompatible-pointer-types]
   18 |     bar(&str);
      |         ^~~~
      |         |
      |         char (*)[9]
<source>:9:17: note: expected 'char (*)[8]' but argument is of type 'char (*)[9]'
    9 | void bar(char (*arg)[8]) { …
Run Code Online (Sandbox Code Playgroud)

c arrays casting undefined-behavior language-lawyer

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

有没有办法在GCC中禁用内联汇编程序?

I'm developing an online judge system for programming contests like LeetCode, Codeforces, etc. As for most programming contests, inline assembler is not allowed in C/C++, so I would like to add the same restriction to my system.

I would like to let GCC and G++ produce an error when compiling a C/C++ program containing inline assembler, so that any code containing inline assembler will result in compilation error.

Is there a way to achieve that? Should I pass some command …

c c++ gcc g++ inline-assembly

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

错误:一元'*'的类型参数无效(具有'long int')

这是我编写的一个小程序,用于打印存储在内存位置(0x7fffdf222fec)的值。 error as 4:20: error: invalid type argument of unary ‘*’ (have ‘long int’ printf("\n %p ",*(0x7fffdf222fec));

我在Ubuntu 16.04中使用gcc编译器,我的ram是4 GB。我不能使用这种方式在此位置访问值吗?

#include<stdio.h>
void main(){
    printf("\n  %p   ",*(0x7ffeae3b3674));
}
Run Code Online (Sandbox Code Playgroud)

预期结果是存储在地址中的垃圾值

我写了一个这样的程序,得到了as的地址

#include<stdio.h>
void main(){
    int a;
    printf("%p",&a);
}
Run Code Online (Sandbox Code Playgroud)

0x7ffeae3b3674

然后,我将上述地址应用于第一个程序。

c pointers

4
推荐指数
2
解决办法
103
查看次数