关于传递指针的工作原理,我有点困惑.
假设我有以下函数和指针,并且......
编辑:
...我想在函数中使用指向某个对象的指针作为参数.
即:
void Fun(int Pointer){
int Fun_Ptr = ---Passed Pointer---;
//So that Fun_Ptr points to whatever ---Passed Pointer points to
Run Code Online (Sandbox Code Playgroud)
在*Pointer和&Pointer符号之间,我很困惑.我知道*指针意味着给它指出的任何东西.
我是否在声明中放入了void(int*pointer).我什么时候使用这个功能?
非常感谢您的协助.
编辑2:
好的,我现在明白在声明中使用*变量意味着将传递指针.但是,当我使用该功能时呢?
即
int main(){
int foo;
int *bar;
bar = foo;
Fun(bar);
}
Run Code Online (Sandbox Code Playgroud)
编辑3: 好的,如果我错了,请纠正我:
根据上述代码的惯例:
bar =&foo表示:使条形指向内存中的foo
*bar = foo表示将条指向的值更改为等于foo等于的值
如果我有第二个指针(int*oof),那么:
bar = oof表示:bar指向oof指针
bar =*oof表示:bar指向oof指向的值,但不指向oof指针本身
*bar =*oof表示:将条指向的值更改为oof指向的值
&bar =&oof表示:更改条指向的内存地址与oof指向的内存地址相同
我有这个权利吗?
编辑4:非常感谢你的帮助(我希望我能接受超过1个答案,但我必须选择第一个答案.我不确定社区维基是如何工作的,但我会这样离开编辑(如果您愿意,可随意将其转换为参考指南).
在编写一个简单的函数来从字符串中删除特定字符时,我遇到了这个奇怪的问题:
void str_remove_chars( char *str, char to_remove)
{
if(str && to_remove)
{
char *ptr = str;
char *cur = str;
while(*ptr != '\0')
{
if(*ptr != to_remove)
{
if(ptr != cur)
{
cur[0] = ptr[0];
}
cur++;
}
ptr++;
}
cur[0] = '\0';
}
}
int main()
{
setbuf(stdout, NULL);
{
char test[] = "string test"; // stack allocation?
printf("Test: %s\n", test);
str_remove_chars(test, ' '); // works
printf("After: %s\n",test);
}
{
char *test = "string test"; // non-writable?
printf("Test: %s\n", …Run Code Online (Sandbox Code Playgroud) int* myPointer = new int[100];
// ...
int firstValue = *(myPointer + 0);
int secondValue = myPointer[1];
Run Code Online (Sandbox Code Playgroud)
有没有之间的功能差异*(myPointer + index)和myPointer[index]?这被认为是更好的做法?
错误如下:
请求'arr'中的成员'begin','end'是非类型int [5],无法从表达式错误中推断出来.
我的代码:
#include <iostream>
using namespace std;
int main()
{
int * mypointer;
int arr[5] = {1,3,5,7,9};
mypointer = arr;
for(auto it = arr.begin(); it != arr.end(); ++it) {
cout<<*mypointer<<endl;
mypointer++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我已经回到C中了解某些东西,但是我很难记住这种内存管理的工作原理.我想要一个指向结构指针数组的指针.
说我有:
struct Test {
int data;
};
Run Code Online (Sandbox Code Playgroud)
那么数组:
struct Test **array1;
Run Code Online (Sandbox Code Playgroud)
它是否正确?我的问题是处理这件事.因此,数组中的每个指针都指向单独分配的内容.但我认为我需要先做到这一点:
array1 = malloc(MAX * sizeof(struct Test *));
Run Code Online (Sandbox Code Playgroud)
我无法理解上述内容.我需要这样做,为什么我需要这样做?特别是,如果我要为指针指向的每个东西分配内存,为指针分配内存意味着什么?
现在说我有一个指向结构指针数组的指针.我现在希望它指向我之前创建的相同数组.
struct Test **array2;
Run Code Online (Sandbox Code Playgroud)
我是否需要像上面那样为指针分配空间,或者我可以这样做:
array2 = array1
Run Code Online (Sandbox Code Playgroud) 请考虑C中的以下代码
int x=100;
int*addr=&x;
Run Code Online (Sandbox Code Playgroud)
我知道addr会存储xA问题的地址,这个问题一直在我脑海中浮现,因为addr指针将有自己的地址,并且可以再次使用&符号运算符进行访问,并且它也会存储在某处,所以我们在这里有无限递归在地址上这样结束了吗?
有什么unsigned int*不同的东西吗int*?我知道unsigned有更高的价值范围.还是,int*甚至不能指向任何unsigned int?
考虑以下结构:
struct s {
int a, b;
};
Run Code Online (Sandbox Code Playgroud)
通常为1,此结构的大小为 8,对齐方式为 4。
如果我们创建两个struct s对象(更准确地说,我们将两个这样的对象写入分配的存储空间),并且第二个对象与第一个对象重叠会怎样?
char *storage = malloc(3 * sizeof(struct s));
struct s *o1 = (struct s *)storage; // offset 0
struct s *o2 = (struct s *)(storage + alignof(struct s)); // offset 4
// now, o2 points half way into o1
*o1 = (struct s){1, 2};
*o2 = (struct s){3, 4};
printf("o2.a=%d\n", o2->a);
printf("o2.b=%d\n", o2->b);
printf("o1.a=%d\n", o1->a);
printf("o1.b=%d\n", o1->b);
Run Code Online (Sandbox Code Playgroud)
这个程序的未定义行为有什么问题吗?如果是这样,它在哪里变得未定义?如果不是UB,是否保证始终打印以下内容:
o2.a=3
o2.b=4
o1.a=1
o1.b=3
Run Code Online (Sandbox Code Playgroud)
特别是,我想知道o1 …
对于C++新手来说,允许const成员函数在类引用的对象(通过指针或引用)上调用非const方法通常会让人感到困惑.例如,以下内容完全正确:
class SomeClass
{
class SomeClassImpl;
SomeClassImpl * impl_; // PImpl idiom
public:
void const_method() const;
};
struct SomeClass::SomeClassImpl
{
void non_const_method() { /*modify data*/ }
};
void SomeClass::const_method() const
{
impl_->non_const_method(); //ok because impl_ is const, not *impl_
};
Run Code Online (Sandbox Code Playgroud)
但是,如果constness传播到尖头对象,它有时会非常方便(我自愿使用PImpl习语,因为它是我认为"constness传播"非常有用的情况之一).
使用指针时,可以通过使用某种智能指针轻松实现这一点,操作符在constness上重载:
template < typename T >
class const_propagating_ptr
{
public:
const_propagating_ptr( T * ptr ) : ptr_( ptr ) {}
T & operator*() { return *ptr_; }
T const & operator*() const { return *ptr_; }
T * operator->() …Run Code Online (Sandbox Code Playgroud) 我遇到了C#的问题,我想在我的代码中得到一个方法指针,但似乎不可能.我需要方法的指针,因为我想使用WriteProcessMemory来禁止它.我怎么得到指针?
示例代码
main()
{
function1();
function2();
}
function1()
{
//get function2 pointer
//use WPM to nop it (I know how, this is not the problem)
}
function2()
{
Writeline("bla"); //this will never happen because I added a no-op.
}
Run Code Online (Sandbox Code Playgroud)