小编Aru*_*A S的帖子

将常量引用传递给函数

我试图使用传递引用将常量整数传递给函数.

#include <iostream>
using namespace std;

int test(int& num);
// changes a constant variable

int main() {
  int loopSize = 7;
  const int SIZE = loopSize;
  cout<<SIZE<<endl;
  test(loopSize);
  cout<<SIZE;
  return 0;
}

int test(int& num){
  num -= 2;
}
Run Code Online (Sandbox Code Playgroud)

但是,输出永远不会更新.

c++

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

字符指针和字符串的大小

我按以下代码测试char数组,指针和字符串的长度/大小。
为什么sizeof(pArray)是8?我猜它应该是4的指针。
为什么sizeof(str)是8而不是6或7?
为什么sizeof("abcdef")是7而不是6?

char array1[10] = {'a', 'b'};
char array[10] = "abcdef";
const char * pArray = "abcdef";
string str = "abcdef";
printf("array1:%d, array:%d, pArray:%d, str:%d,strsize:%d, strlen:%d,  raw:%d\n", sizeof(array1), sizeof(array), sizeof(pArray), sizeof(str), str.size(), str.length(), sizeof("abcdef"));
Run Code Online (Sandbox Code Playgroud)

程序输出:

array1:10, array:10, pArray:8, str:8,strsize:6, strlen:6,  raw:7
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

C11 atomic_load/store和volatile限定符

为什么C11 atomic_load/ atomic_store函数中的参数具有volatile限定符?因为它是公认的volatile是无用的并发使用情况下(例如1,2).

atomic_load( const volatile A* obj );
void atomic_store( volatile A* obj , C desired);
Run Code Online (Sandbox Code Playgroud)

c concurrency atomic

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

什么时候使用*这个?

您好我正在学习类,我有以下代码:

Sales_Data & Sales_Data::combine(const Sales_Data & rhs){
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;  }
Run Code Online (Sandbox Code Playgroud)

我想知道为什么我们使用return *this,和 Sales_Data &.

c++

0
推荐指数
2
解决办法
206
查看次数

C,获取分段错误

我有一个名为islands.txt的文件,内容如下:

islandone
islandtwo
islandthree
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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

typedef struct island{
    char *name;
    struct island *previous;
} island;

void printIsland(island is){
    printf("%s", is.name);
    if(is.previous && is.previous->name[0] != '\0'){
        printf("%s", is.previous->name);
    }
}

int main(){

    // the file to be read.
    FILE *islandsFile = fopen("islands.txt","r");

    // temporary location to store the name read from the file.
    char name[40];

    // temporary pointer to an island which has been already read for linking.
    island *previousIsland;

    while(fscanf(islandsFile,"%s",name) != EOF){
        // allocate …
Run Code Online (Sandbox Code Playgroud)

c segmentation-fault

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

无限循环中的内存泄漏

如果您运行的程序不断产生内存泄漏,您的计算机有什么损害?

例如:

while(true)
{
    char* c = malloc(sizeof(char));
    c = NULL;
}
Run Code Online (Sandbox Code Playgroud)

然后让代码执行数小时或数天?

c memory-leaks infinite-loop

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

比较和定义C中的字符串

我查看了其他答案,但未能找到答案.
我想将字符串的最后一个字符与文字"%"进行比较.

  strcpy(ch, fName[strlen(fName) - 1]);
  printf("%d\n", strcmp(ch, "%"));
Run Code Online (Sandbox Code Playgroud)

我想用cl命令行编译器(MicroSoft)编译,并获得警告:

cbx_test1.c(43) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char'
cbx_test1.c(43) : warning C4024: 'strcpy' : different types for formal and actual parameter 2
Run Code Online (Sandbox Code Playgroud)

我的代码有问题,这是显而易见的.但是什么?

c

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

如何从具有NULL的unsigned char数组写入数据?

我不太用C,所以这让我很难过.

unsigned char[255]需要通过TCP/IP发送一些数据(更大的结构的一部分,类型是).我无法控制我获取数据的格式.

一小部分数据:

    [0] 114 'r' unsigned char
    [1] 44 ','  unsigned char
    [2] 0 '\0'  unsigned char
    [3] 68 'D'  unsigned char
    [4] 85 'U'  unsigned char
    [5] 255 'ÿ' unsigned char
    [6] 128 '€' unsigned char
    [7] 128 '€' unsigned char
Run Code Online (Sandbox Code Playgroud)

数据以分割的其他信息为前缀'|',因此我的尝试是将所有信息写入缓冲区,也可以使用第三方软件发送无符号.

问题似乎是数据中有数据'\0',它会以任何方式将其写入缓冲区.

我已经尝试迭代数组,获取每个char并尝试strcat将char添加到缓冲区,这仅适用于非'\0'字符.

我已经尝试strcat了整个阵列,当它到达时也会停止'\0'

因此,非常感谢任何帮助或推动正确的方向.

c

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