在stackoverflow的上一个问题中的一段代码中,我看到了这个,对我来说很奇怪,声明using
:
template <std::size_t SIZE>
class A
{
public:
...
using const_buffer_t = const char(&)[SIZE];
...
};
Run Code Online (Sandbox Code Playgroud)
有人可以解决以下问题:
Activity solution[a][b];
...
Activity **mother = solution;
Run Code Online (Sandbox Code Playgroud)
我想将2D对象数组转换为指向指针的指针.我怎样才能做到这一点;
我在谷歌搜索它.但是我发现只有一个维数组的例子.
int array[10];
int* a = array + 10; // well-defined
int* b = &array[10]; // not sure...
Run Code Online (Sandbox Code Playgroud)
最后一行是否有效?
我试图理解"指向成员的指针"是如何工作的,但对我来说并非一切都清楚.
这是一个示例类:
class T
{
public:
int a;
int b[10];
void fun(){}
};
Run Code Online (Sandbox Code Playgroud)
以下代码说明问题并包含问题:
void fun(){};
void main()
{
T obj;
int local;
int arr[10];
int arrArr[10][10];
int *p = &local; // "standard" pointer
int T::*p = &T::a; // "pointer to member" + "T::" , that is clear
void (*pF)() = fun; //here also everything is clear
void (T::*pF)() = T::fun;
//or
void (T::*pF)() = &T::fun;
int *pA = arr; // ok
int T::*pA = T::b; // error
int (T::*pA)[10] …
Run Code Online (Sandbox Code Playgroud) 我的代码如下:
main() {
int array[5] = {3,6,9,-8,1};
printf("the size of the array is %d\n", sizeof(array));
printf("the address of array is %p\n", array);
printf("the address of array is %p\n", &array);
int * x = array;
printf("the address of x is %p\n", x);
printf("the size of x is %d\n", sizeof(x));
}
Run Code Online (Sandbox Code Playgroud)
输出是
the size of the array is 20
the address of array is 0x7fff02309560
the address of array is 0x7fff02309560
the address of x is 0x7fff02309560
the size of x is …
Run Code Online (Sandbox Code Playgroud) 我使用以下模板获取指向数组的最后一个元素后面的指针:
template <typename T, size_t n>
T* end_of(T (&array)[n])
{
return array + n;
}
Run Code Online (Sandbox Code Playgroud)
现在我似乎记得这种方法存在一些问题,但我不记得它是什么.我相信它与类型参数或函数参数的选择有关,但我不确定.所以就像进行健全性检查一样,您是否看到上述代码存在任何问题?小用量测试:
int test[] = {11, 19, 5, 17, 7, 3, 13, 2};
std::sort(test, end_of(test));
Run Code Online (Sandbox Code Playgroud) int my_array[5] = {0};
int *my_pointer = 0;
my_pointer = &my_array // compiler error
my_pointer = my_array // ok
Run Code Online (Sandbox Code Playgroud)
如果my_array
是数组的地址那么什么&my_array
给了我?
我想得到一个数组的长度int array[] = {1, 2, 3, 4}
.我曾经sizeof
这样做过.
int length(int array[])
{
return sizeof(array) / sizeof(int);
}
int main()
{
int array[] = {1, 2, 3, 4};
printf("%d\n", length(array)); // print 1
printf("%d\n", sizeof(array) / sizeof(int)); // print 4
}
Run Code Online (Sandbox Code Playgroud)
那么,为什么sizeof(array)
in函数length
返回指针大小array
?但在功能方面main
,它确实有效.
而且,我应该如何修改length
函数以获得数组的长度?
我写了一个简单的C++程序来反转一个字符串.我在字符数组中存储一个字符串.要反转字符串,我使用相同的字符数组和临时变量来交换数组的字符.
#include<iostream>
#include<string>
using namespace std;
void reverseChar(char* str);
char str[50],rstr[50];
int i,n;
int main()
{
cout<<"Please Enter the String: ";
cin.getline(str,50);
reverseChar(str);
cout<<str;
return 0;
}
void reverseChar(char* str)
{
for(i=0;i<sizeof(str)/2;i++)
{
char temp=str[i];
str[i]=str[sizeof(str)-i-1];
str[sizeof(str)-i-1]=temp;
}
}
Run Code Online (Sandbox Code Playgroud)
现在这个方法不起作用,我在程序执行后获得NULL String作为结果.
所以我想知道为什么我不能将字符数组等同,为什么这个程序不起作用.我可以使用什么解决方案或技巧来使相同的程序工作?
您好我是c ++的初学者,有人可以向我解释一下
char a[]="Hello";
char b[]=a; // is not legal
Run Code Online (Sandbox Code Playgroud)
然而,
char a[]="Hello";
char* b=a; // is legal
Run Code Online (Sandbox Code Playgroud)
如果无法将数组复制或分配给另一个数组,为什么它可以作为参数传递,其中传递的值的副本始终在方法中进行
void copy(char[] a){....}
char[] a="Hello";
copy(a);
Run Code Online (Sandbox Code Playgroud)