相关疑难解决方法(0)

为什么数组会衰减到模板函数中的指针

我不明白为什么数组衰减到模板函数中的指针.

如果您查看以下代码:当参数被强制为参考(函数f1)时,它不会衰减.在另一个函数中,它衰减了.为什么函数f中的T类型不是const char(buff&)[3]而是const char*(如果我理解正确的话)?

#include <iostream>

template <class T>
void f(T buff) {
    std::cout << "f:buff size:" << sizeof(buff) << std::endl;       //prints 4
}

template <class T>
void f1(T& buff) {
    std::cout << "f:buff size:" << sizeof(buff) << std::endl;       //prints 3
}

int main(int argc, char *argv[]) {
    const char buff[3] = {0,0,0};
    std::cout << "buff size:" << sizeof(buff) << std::endl;         //prints 3
    f(buff);
    f1(buff);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays templates sizeof

7
推荐指数
2
解决办法
1825
查看次数

把一个va_list变量放在......变量参数列表(!)中

我想设计一个函数,它接受可变数量的参数,其中一个参数本身就是一个va_list; 但我的代码出了问题,我不明白...

警告 - 我的问题不是设计代码做我想做的事情(我找到了绕过问题的方法),而只是关于理解我做错了什么......

为了解释我的问题,让我们从一个简单的例子开始:即一个函数ffprintf,它的作用类似于fprintf,但将其内容写入多个字符串,其编号由第一个参数表示的字符串,ffprintf其身份由下一个参数给出(这些参数的数量可能因调用而异,因此您必须使用变量参数列表).这样的函数将使用如下:

FILE *stream0, *stream1, *stream2;
int a, b;
ffprintf (3, stream0, stream1, stream2, "%d divided by %d worths %f", a, b, (double)a / b);
Run Code Online (Sandbox Code Playgroud)

它的代码是:

void ffprintf (int z, ...)
 {va_list vlist, auxvlist;
  FILE **streams = malloc (z * sizeof(FILE *));
  va_start (vlist, z);
  for (int i = 0; i < z; ++i)
   {streams[i] = va_arg (vlist, FILE *); // Getting the next stream argument
   }
  char …
Run Code Online (Sandbox Code Playgroud)

c variadic-functions

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

C:为什么可以按值传递(给函数)结构体,而不是数组?

这背后有什么历史或逻辑原因吗?


解释:当您将数组传递给 C 中的函数时,您实际上只传递了一个指向数组的指针。但是,当您传递结构时,您可以传递结构的副本或指针。

//this:
int function(int array[10])
// is equivalent to this:
int function(int *array)
//and they both pass a pointer

//but this:
int function(struct tag name)
//passes a struct by value, where as this:
int function(struct tag *name)
//passes a pointer to it.
Run Code Online (Sandbox Code Playgroud)

为什么会有差异?

c arrays struct pass-by-value

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

将数组和结构传递给C函数

我想知道为什么我们可以通过值将结构传递给C函数,但我们永远不能对数组(通过地址传递)做同样的事情.

当我学习C时,他们告诉我数组会消耗很多堆栈,因此不希望按值传递它们.

但似乎结构通常(如果不总是)大于数组并且是更复杂的数据结构,所以这个解释现在对我来说没有意义!

任何人都可以帮助尽可能多的细节吗?

arrays structure function memory-address

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

二维数组不会衰减为指向指针的指针

到目前为止我非常确定

int arr[4][5];
Run Code Online (Sandbox Code Playgroud)

然后arr将衰减为指针到指针。

这个链接证明我错了。

我不确定我是如何arr成为指针到指针的,但这对我来说似乎很明显。因为arr[i]将是一个指针,因此arr应该是一个指向指针的指针。

我是不是错过了什么。

c arrays pointers

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

有没有办法创建一个不会改变程序参数的函数

我创建了一个代码,我想在2个函数中使用相同的变量,但是我不想让功能将值更改为其他函数。为了使自己更清楚,这里是一个示例:

int num1(int arr[5][6],int count);
int num2(int arr[5][6],int count2);
   int main()
   {
       int count = 0;
    int count2 = 0;
    int arr[5][6] = {
   {0, 0, 0, 1, 0, 0} ,   
   {0, 0, 0, 0, 0, 0} ,   
   {0, 0, 0, 0, 0, 0} ,
   {0, 0, 0, 0, 0, 0} ,
   {0, 0, 0, 0, 0, 0}
    };
    cout << num1(arr,count);
    cout << num2(arr,count2);
    return 0;
    }
int num1(int arr[5][6],int count){
    for (int i = 0; i < 5; …
Run Code Online (Sandbox Code Playgroud)

c++

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

C++字符串交换.它为什么有效?

基本上,复制字符串函数应该将1个字符串复制到另一个字符串.这两个字符串的输出是Hello World!为什么这实际上有效,因为我只传递这些字符串的值?我不是通过参考或任何东西.

我的第二个问题是为什么我必须包含string.h而不是iostream.h?为什么.h部分只需要包含字符串头文件?为什么iostream如此特别,它不需要.h扩展名?

#include <iostream>
#include <string.h>

void copyString(char[] stringToCopyTo, char[] stringToCopy);

int main(){

    char string1[] = "Hello World!";
    char string2[80];

    copyString(string2, string1);

    std::cout << "String1: " << string1 << "\n";
    std::cout << "String2: " << string2 << "\n";

    return 0;
}

void copyString(char stringToCopyTo[], char stringToCopy[]){
    stringToCopyTo = stringToCopy;
}
Run Code Online (Sandbox Code Playgroud)

c++ string

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