相关疑难解决方法(0)

char s []和char*s有什么区别?

在C中,可以在声明中使用字符串文字,如下所示:

char s[] = "hello";
Run Code Online (Sandbox Code Playgroud)

或者像这样:

char *s = "hello";
Run Code Online (Sandbox Code Playgroud)

那么区别是什么呢?我想知道在编译和运行时的存储持续时间实际发生了什么.

c string constants char

495
推荐指数
6
解决办法
33万
查看次数

C++警告:不推荐将字符串常量转换为'char*'[-Wwrite-strings]

我正在使用gnuplot在C++中绘制图形.该图正如预期的那样绘制,但在编译期间会出现警告.警告意味着什么?

warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的功能:

void plotgraph(double xvals[],double yvals[], int NUM_POINTS)
{
    char * commandsForGnuplot[] = {"set title \"Probability Graph\"", 
        "plot     'data.temp' with lines"};
    FILE * temp = fopen("data.temp", "w");
    FILE * gnuplotPipe = popen ("gnuplot -persistent ", "w");
    int i;
    for (i=0; i < NUM_POINTS; i++)
    {
        fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]); 
        //Write the data to a te  mporary file
    }
    for (i=0; i < NUM_COMMANDS; i++)
    {
        fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); …
Run Code Online (Sandbox Code Playgroud)

c++ string-literals

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

const char*是用C++编写的

C++中的字符串表达式如何工作?

考虑:

#include <iostream>
using namespace std;

int main(int argc, char *argv[]){

    const char *tmp="hey";
    delete [] tmp;

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

存储"嘿"表达式的位置和方式,以及为什么在尝试删除它时会出现分段错误?

c++ string

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

标签 统计

c++ ×2

string ×2

c ×1

char ×1

constants ×1

string-literals ×1