考虑到这个声明:
int 数组[] = ....;
我知道&array[0]将第一个元素的地址作为其值。
但是&array呢?我在我的程序中尝试了这个,它还返回了第一个元素的地址。那么有什么区别呢?
最近看到一段C代码是这样的:
#include <stdio.h>
int main(void) {
int array[5] = {1, 2, 3, 4, 5};
for (int* ptr = &array[0]; ptr != &array[5]; ptr++)
printf("%d\n", *ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于运算[]符&在 C 中优先于运算符,因此我认为&array[5]等效于&(*(array + 5)),这会导致未定义的行为(我们不允许取消引用array + 5)。这就是为什么我怀疑上面的代码格式错误。(顺便说一下,我知道这ptr != array + 5没关系。)
我使用带有-O0 -fsanitize=address,undefined编译器标志的GCC 11.1.0 和 Clang 12.0.0 测试了这段代码,但两个编译器都解释&array[5]为array + 5,并且没有发生意外行为。
是否&array[i]总是等同于array + i(即使array[i]是无效的)?先感谢您。
是否有可能在C++中做这样的事情(现在不能自己测试)?
int myarray[10] = {111,222,333,444,555,666,777,888,999,1234};
void functioncc()
{
int temparray = myarray;
for(int x=0; x<temparray.length; x++){
.... do something
}
}
Run Code Online (Sandbox Code Playgroud)
也许这个(但我不认为是):
int array1[5] = {0,1,2,3,4,5,6,7,8,9};
int array2[5] = {9,8,7,6,5,4,3,2,1,0};
void functioncc(int arid)
{
temparray[10] = "array"+arid;
........
}
Run Code Online (Sandbox Code Playgroud)
我可以在JavaScript中做类似的事情,但就像我说的那样 - 不要认为它在C++中是可能的.
谢谢你的时间.
假设我在C++中有以下内容:
char buffer[SIZE];
char * ptr = &buffer[SIZE];
Run Code Online (Sandbox Code Playgroud)
哪里ptr的价值永远不会被取消引用.这样做C++是否合法?那是使用从数组的最后一个元素开始的内存地址(比如作为要比较的特殊值)?
至少使用我的编译器,创建引用意味着不会解除引用.因此,代码如下所示:
int trivialExample(char* array, int length)
{
char& ref = array[6];
if (length > 6)
{
std::cout << ref << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,给定一个char数组及其长度(并假设像数组元素这样的一些小数都被初始化并且传递的长度是正确的),它将仅在它实际存在时打印第七个字符.
这是依赖于未定义的行为吗?
在c ++中是否已经很好地定义了一个指向数组类型的一个过去的指针?
请考虑以下代码:
#include <cassert>
#include <iterator>
int main()
{
// An array of ints
int my_array[] = { 1, 2, 3 };
// Pointer to the array
using array_ptr_t = int(*)[3];
array_ptr_t my_array_ptr = &my_array;
// Pointer one-past-the-end of the array
array_ptr_t my_past_end = my_array_ptr + 1;
// Is this valid?
auto is_this_valid = *my_past_end;
// Seems to yield one-past-the-end of my_array
assert(is_this_valid == std::end(my_array));
}
Run Code Online (Sandbox Code Playgroud)
常识是,取消引用一个过去的指针是未定义的行为.但是,对于指向数组类型的指针,这是否适用?
这似乎是合理的,这应该是有效的,因为*my_past_end完全可以利用指针算法来解决,并产生一个指向数组中的第一个元素会在那里,这恰好也是一个有效的,过去最末端int*的原始数组my_array.
但是,查看它的另一种方法*my_past_end是生成对不存在的数组的引用,该数组隐式转换为 …
我正在编写一个简单的字符串连接程序.
该程序按我发布的方式工作.但是,我首先使用以下代码编写它来查找字符串的结尾:
while (*s++)
;
Run Code Online (Sandbox Code Playgroud)
但是,该方法不起作用.传递给它的字符串未正确复制.具体来说,我试图将"abc"复制到一个持有"\ 0"的char []变量.
从阅读C K&R书籍看起来应该可行.紧凑的表格应采取以下步骤.
那为什么不起作用呢?我在Debian上用gcc编译.
我发现这个版本确实有效:
strncat(char *s, const char *t, int n)
{
char *s_start = s;
while (*s)
s++;
for ( ; n > 0 && *t; n--, s++, t++)
*s = *t;
*(s++) = '\0';
return s_start;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
例如,如果我有一个包含5个输入元素的数组,那么如果已经在变量中建立了该值,我将如何计算输入特定值的次数.
INPUT:
4
4
4
1
2
Run Code Online (Sandbox Code Playgroud)
如果click定义为4那么我如何计算click数组中使用了多少次?希望这是有道理的.谢谢
请考虑以下简单代码对数组进行排序.
int myarray[4] = {};
std::sort(myarray, myarray + 4);
Run Code Online (Sandbox Code Playgroud)
我知道创建一个指向C风格数组末尾的指针是有效的.
我最近看过这样的代码:
std::sort(myarray, &myarray[4]);
Run Code Online (Sandbox Code Playgroud)
我不确定这是否有效,因为它取消引用数组边界之外的元素,即使元素值不用于任何东西.
这是有效的代码吗?
这是未定义的行为吗?
ptrdiff_t one() {
std::vector<int> test(1);
return &test[1] - &test[0];
}
Run Code Online (Sandbox Code Playgroud)
这是未定义的行为吗?
ptrdiff_t zero() {
std::vector<int> test;
int * end = &test[0];
int * begin = &test[0];
return end - begin;
}
Run Code Online (Sandbox Code Playgroud)
如果其中任何一个是未定义的行为,任何人都可以帮我找到C++ 11规范中的部分,它描述了向量的下标运算符必须在小于(而不是小于或等于)大小的值上调用, 或相反亦然?
谢谢
这是我一直试图运行的简单而简短的代码:
#include <stdio.h>
int const SIZE = 5;
void a(int *arr);
int main(){
int arr[5] = {1,2,3,4,5};
a(arr);
return 0;
}
void a(int *arr){
int *i;
for (i=arr; i<&a[5]; i++)
printf("%d",*arr[i]);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误/警告:
main.c: In function ‘main’:
main.c:15: error: variable-sized object may not be initialized
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c:15: warning: excess elements in array initializer
main.c:15: warning: …Run Code Online (Sandbox Code Playgroud) 我想知道是否访问其边界之外的数组(以下代码示例中的第2行)是否会产生错误?
int a[20];
int* ptr = &a[20]; // line 2
int count=20;
do
{
ptr--;
printf("%d",*ptr);
}while(--count!=0);
Run Code Online (Sandbox Code Playgroud) 我正在使用教程point.com示例.
int var[MAX] = {10, 100, 200};
int *ptr;
// let us have address of the last element in pointer.
ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// point to the previous location
ptr--;
}
return 0;
Run Code Online (Sandbox Code Playgroud)
那么,&var[MAX - 1] 为什么不 …