这种方法适用于C++,但不适用于C
它给了我这个错误 initializer element is not constant
#include <stdio.h>
int n = printf("Hello World");
int main() {}
Run Code Online (Sandbox Code Playgroud)
如何hello world使用空main函数打印?
我只需要将此C++代码转换为C
#include <iostream>
using namespace std;
int n = printf("Hello World");
int main() {}
Run Code Online (Sandbox Code Playgroud)
或这个
#include <iostream>
using namespace std;
int fun()
{
cout << "Hello World";
return 1;
}
int x = fun();
int main() {}
Run Code Online (Sandbox Code Playgroud)
或者这个 C++ 代码
#include <iostream>
using namespace std;
int fun()
{
cout << "Hello …Run Code Online (Sandbox Code Playgroud) 所以我有两个结构体,为了简单起见,我们将它们称为A和B。A包含一个指向 的指针B,并B包含一个A。所以这里是代码:
啊
#ifndef A_H
#define A_H
#include "b.h"
typedef struct _A {
B *b;
} A;
#endif
Run Code Online (Sandbox Code Playgroud)
哈
#ifndef B_H
#define B_H
#include "a.h"
typedef struct _B {
A a;
} B;
#endif
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我从我的主 c 文件中导入 ah 时,我收到关于 A 是如何从 bh 未知类型的错误我不知道如何解决这个问题。
我不明白这里发生了什么。一定有一些与“冲洗”术语有关的东西,但我想要一个解释。
int arr[4] {3,8,1,6};
cout<<arr[0];
cout<<arr[1];
cout<<arr[2];
cout<<arr[3];
cout<<endl;
cout<<&arr[0]<<endl;
cout<<&arr[1]<<'\n';
cout<<&arr[2]<<endl;
cout<<&arr[3]<<endl;
cout<<&arr[0]<<endl;
int *j = &arr[0];
cout << *j << *(++j) << *(++j) << *(++j); // HERE IS THE PROBLEM
Run Code Online (Sandbox Code Playgroud)
为什么最后一条cout指令倾向于输出相反的数字?在我看来,我喜欢回溯,但我不确定。
我期待3816作为输出,而不是我得到6618.
这应该是一个递归函数,当 i 小于 'h' 时,它不断向 'h' 添加 1/i 的值,但我在函数声明中不断收到冲突类型错误,我做错了什么?它只是发生在浮动中。
#include <stdio.h>
int main()
{
int i=0, n = 5;
float h;
division(i, n, h);
return 0;
}
float division(int i, int n, float h)
{
if(i<n)
{
h += 1/i;
division(i, n, h);
}
else
{
printf("the sum is: %f",h);
return h;
}
}
Run Code Online (Sandbox Code Playgroud) c floating-point runtime-error division function-declaration
我有这个交换通过引用传递的整数的函数,它在 C++ 中工作正常,但在 C 中不起作用。
#include <stdio.h>
void swap(int & x, int & y)
{
int z = x;
x = y;
y = z;
}
int main()
{
int a = 0, b = 1;
swap(a, b);
printf("a is now %d\n", a);
printf("b is now %d\n", b);
}
Run Code Online (Sandbox Code Playgroud)
为什么它在 C 中不起作用?
printf()C 中的函数\n最后是否需要 a才能工作?
我尝试打印出一个最后没有换行符的简单语句,但没有奏效。
谢谢。
我遇到了一些代码,其中包含以下示例的几个实例:
std::string str = "This is my string";
char* ptr = const_cast<char*>(str.c_str());
ptr[5] = 'w';
ptr[6] = 'a';
Run Code Online (Sandbox Code Playgroud)
在这个简化的例子中,有一个 的赋值std::string::c_str(),它返回const char*到一个char*使用的指针const_cast。
这似乎按预期工作并相应地str进行了修改。
但std::string::c_str()在我当地图书馆的描述如下:
返回指向空终止内容的常量指针。这是内部数据的句柄。不要修改,否则可能会发生可怕的事情。
在cppreference 中,描述包括:
返回一个指向空终止字符数组的指针,该数组的数据与存储在字符串中的数据等效。
...
写入通过访问的字符数组
c_str()是未定义的行为。
在标准的[string.accessors]的descritpion是类似的,没有关于这个信息......空终止字符数组与等同于存储在所述字符串中的数据被提供。
这不是澄清问题,而是让我更加困惑,什么字符数组?它是如何等效的?是副本吗?如果是这样,为什么要修改它,还要修改原始字符串?这个实现定义了吗?
我想避免更改使用它的所有代码实例的任务,所以我的问题是:
有什么办法可以是正确的,使用ptr指针修改str字符串是否合法?
删除ptr后,是否cout << ptr打印 的地址int(6)?
如果是的话,为什么会出现乱码呢?我记得是delete只释放指定空间的数据吧?
还有我想问delete这里释放空间数据的时候,是只释放6还是连int类型都释放?
int* ptr = new int(6);
cout << "Address of the space pointed to by ptr: " << ptr << endl;
cout <<"the value of the space pointed to by ptr: "<< *ptr << endl;
delete ptr;
cout << ptr << endl;
Run Code Online (Sandbox Code Playgroud) 代码:
#include <stdio.h>
void Print(char str[100])
{
printf("%s", str);
}
int main(void)
{
Print("A");
}
Run Code Online (Sandbox Code Playgroud)
使用 GCC 11.1 及更高版本编译时会产生警告
警告:“打印”访问大小为 2 的区域中的 100 个字节 [-Wstringop-overflow=]
当我传递一个较小的字符串时。这是没有启用任何警告标志的情况。
示例代码: https: //godbolt.org/z/nWs1PK9Y6
正如您所看到的,其他编译器对此并不抱怨。
我确实发现了一个有相关问题的线程,但是关于 int 数组,这更奇怪:
当不使用字符串操作时,GCC 11 给出 -Wstringop-overflow
然而,这绝对是一个错误,已被纠正,并且在 trunk 版本中不再出现,而在代码中我显示它在 trunk 中仍然有相同的警告。
我相信这是另一个错误,或者至少是一个不太有用的警告,因为据我所知,该参数将衰减为指针,但我想确认这一点。
正如评论部分所述,这对于警告 API 用户正在作为参数传递较小的字符串很有用,但是它真的有意义吗,至少在没有启用警告标志的情况下显示它?
在其他情况下,将字符串传递给更大的数组是微不足道的。对我来说,如果传递的字符串大于参数应该采用的字符串,则警告用户更有意义,但如果我传递的字符串大于 100,警告就会消失。
假设我有从二进制文件(如 ext2 格式的文件系统图像文件)读取的 char 数组。
现在我需要读取从偏移字节 1024 开始的整数(<--这是从数据开始的偏移量)。有什么巧妙的方法可以做到这一点吗?该整数可以是任何数字。所以我相信在我的系统(x86-64)上可以用 4 字节的整数大小来表示。
我相信我需要使用 strtol ,例如:
/* Convert the provided value to a decimal long */
char *eptr=malloc(4);// 4 bytes becuase sizeof int is 4 bytes
....
int valread=read(fd,eptr,4);//fd is to ext2 formatted image file (from file system)
result = strtol(eptr, &v, 10);
Run Code Online (Sandbox Code Playgroud)
上面是long那么这个数字是代表32位整数吗?
应该eptr以 null 终止吗?
这是否正确?