相关疑难解决方法(0)

为什么这个C++片段编译(非void函数不返回值)

今天早上我在我的一个图书馆找到了这个:

static tvec4 Min(const tvec4& a, const tvec4& b, tvec4& out)
{
    tvec3::Min(a,b,out);
    out.w = min(a.w,b.w);
}
Run Code Online (Sandbox Code Playgroud)

我期望编译器错误,因为此方法不返回任何内容,并且返回类型不返回void.

想到的唯一两件事是

  • 在调用此方法的唯一位置,不使用或存储返回值.(此方法应该是void- tvec4返回类型是复制和粘贴错误)

  • tvec4正在创建一个默认构造,这看起来有点不同,哦,C++中的其他所有东西.

我还没有找到解决这个问题的C++规范部分.参考文献(ha)表示赞赏.

更新

某些情况下,这会在VS2012中生成错误.我没有缩小具体细节,但它仍然很有趣.

c++ methods c++11 visual-studio-2012

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

没有定义类型的函数参数默认为int?我疯了吗?

由于一些奇怪的原因,我正在复制另一种不使用类型的语言的例子,并忘了在函数定义参数中添加一个,并且它有效.

#include <stdio.h>

char toChar(n) {
  //sizeof n is 4 on my 32 bit system
  const char *alpha = "0123456789ABCDEF";
  return alpha[n];
}

int main() {
  putchar(toChar(15)); //i.e.
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我确信某些标准的大多数编译器的main默认为int(但只返回),对于其他函数,这也是一种行为,或者这个实现是否定义了?这似乎与众不同,我的编译器只是一个稍微过时的GCC端口(MinGW).

c standards function

9
推荐指数
2
解决办法
4264
查看次数

C:警告:赋值使得整数指针没有强制转换[默认启用]

这是我的代码

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

void main() {
    FILE *fp;
    char * word;
    char line[255];
    fp=fopen("input.txt","r");
    while(fgets(line,255,fp)){
        word=strtok(line," ");
        while(word){
            printf("%s",word);
            word=strtok(NULL," ");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的警告.

token.c:10:7: warning: assignment makes pointer from integer without a cast [enabled by default]
   word=strtok(line," ");
       ^

token.c:13:8: warning: assignment makes pointer from integer without a cast [enabled by default]
    word=strtok(NULL," ");
        ^
Run Code Online (Sandbox Code Playgroud)

word声明为char*.那为什么会出现这个警告呢?

c strtok

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

为什么我可以使用“静态”限定符声明一个没有类型的变量?(在C中)

在学习staticqualifier时C,我错误地编写了以下代码。我认为该getEven()函数不会被编译,但是效果很好。为什么我可以声明没有类型的变量?

我尝试了一些测试,发现static没有类型的变量的类型是4字节整数。

//This code works well.
int getEven(int i) {
static int counter = 0;
if (i%2==0) {
    counter++;
}
    return counter;
}

//I thought this code would make compile error, but it also works well.
int getEven_(int i) {
static counter = 0; //No type!
if (i % 2 == 0) {
    counter++;
}
    return counter;
}
Run Code Online (Sandbox Code Playgroud)

c

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

linux上的c函数

我实际上是在Ubuntu 18.04上使用C语言.我不使用任何IDE.

#include <stdio.h>

void main()
{
    message();
    printf("\nCry, and you stop the monotomy!\n");
}

void message()
{
    printf("\nSmile, and the worldsmiles with you...");
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它返回错误消息,如下所示.

msg.c: In function ‘main’:

msg.c:5:2: warning: implicit declaration of function ‘message’ [-Wimplicit-function-declaration]

  message();

  ^~~~~~~

msg.c: At top level:

msg.c:8:6: warning: conflicting types for ‘message’

 void message()

      ^~~~~~~

msg.c:5:2: note: previous implicit declaration of ‘message’ was here

  message();

  ^~~~~~~
Run Code Online (Sandbox Code Playgroud)

当我把消息函数放在上面时,main()它显示没有错误.为什么?我们不能把功能放到后面main()吗?什么是隐含声明?

c function

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

标签 统计

c ×4

function ×2

c++ ×1

c++11 ×1

methods ×1

standards ×1

strtok ×1

visual-studio-2012 ×1