小编Pie*_*son的帖子

printf() 混淆 - 尝试打印字符串时打印(null),并在打印缓冲区时打印正确的值

查看简化的代码 - 我很困惑......

#include <stdio.h>
#include <string>      
#include <cstdlib>  // includes the standard library and overarchs over stdlib.h

using namespace std;  
void main()
{
    char buffer[10];
    string theString;
    int i = 997799; //(simplified)
    itoa(i,buffer,10);                      
    theString = buffer;             

    printf("\n string is: %s of length %d \n", theString, theString.length());
    printf("\n buffer is: %s of length %d \n", buffer, theString.length());
    return;
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是出乎意料的:

string is: (null) of length 926366009
buffer is: 997799 of length 6
Run Code Online (Sandbox Code Playgroud)

(1)为什么字符串打印为null?

(2) 为什么 theString.length() 在第一个 printf() 中打印不正确,但在第二个 printf() …

c++ string null printf

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

0xC0000005:访问冲突写入位置0xcccccccc

当我调用我的calculatePlaneEQ函数时,当我分配值时,它会抛出一个访问冲突...我的指针生锈了,但似乎这应该有效!

float *planeCoefA, *planeCoefB, *planeCoefC, *planeCoefD = NULL;
Run Code Online (Sandbox Code Playgroud)

呼叫:

calculatePlaneEQ (<...>, &planeCoefA, &planeCoefB, &planeCoefC, &planeCoefD);
Run Code Online (Sandbox Code Playgroud)

DEF:

void calculatePlaneEQ (<...>, float ** myXnorm, float ** myYnorm, float ** myZnorm, float** myD)
{
    float  xNorm = 1.3;
    float  yNorm = 1.4;
    float  zNorm = 1.5;
    float eqD = 1.6; 

    *(*myXnorm) = xNorm;
    *(*myYnorm) = yNorm;
    *(*myZnorm) = zNorm;
    *(*myD) = eqD;  
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers pass-by-reference

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

标签 统计

c++ ×2

null ×1

pass-by-reference ×1

pointers ×1

printf ×1

string ×1