所以我正在尝试使用指针,因为我对C++编程很陌生,错误p1未在此范围内声明不断出现我不知道我在互联网最深处的角落搜索无济于事.
#include <iostream>
using namespace std;
int main()
{
int num1 = 8;
*p1 = &num1;
cout << "VALUE:" << *p1 <<endl;
cout << "adress" << &num1 <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我编译代码时收到下面报告的错误.你能错误地纠正我吗?
无效的类型参数
->(有int)
我的代码如下:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef struct bundles
{
char str[12];
struct bundles *right;
}bundle;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
unsigned long N;
scanf("%lu", &N);
bundle *arr_nodes;
arr_nodes = malloc(sizeof(bundle)*100);
int i=5;
for(i=0;i<100;i++)
{
scanf("%s", &arr_nodes+i->str);
printf("%s", arr_nodes+i->str);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在这些方面面临问题:
scanf("%s", &arr_nodes+i->str);
printf("%s", arr_nodes+i->str);
Run Code Online (Sandbox Code Playgroud) 我试图调用函数testaddresscall(),其定义如下:
void testaddresscall()
{
printf("success");
}
int main(void)
{
void(*testaddresscallfunc)(void);
testaddresscallfunc= &testaddresscall;
cout << *testaddresscallfunc; //it printed 0x012D2050
typedef void testfunc(void);
testfunc* callbyaddress = (testfunc*)0x012D2050;
callbyaddress();
}
Run Code Online (Sandbox Code Playgroud)
然后发生这种情况
test.exe中0x012D2050处的未处理异常:0xC0000005:访问冲突执行位置0x012D2050.
我在C++中有这个父类
//ParentClass header file
public ParentClass{
public:
ParentClass();
virtual void someParentFunction();
private:
//other member variables and functions
};
//Functions implemented done in respective .cpp file
Run Code Online (Sandbox Code Playgroud)
我扩展了这个课程,所以我有一个看起来像这样的孩子
//ChildOneClass header file
public ChildOneClass : public ParentClass{
public:
//Constructors and other functions
private:
//Other members
};
//Functions implemented in respective .cpp file
Run Code Online (Sandbox Code Playgroud)
示例声明:
//Dynamically create one ChildOneClass object
ChildOneClass * c = new ChildOneClass();
//I know this is never done, but for example purposes i just did this
void * v = c; …Run Code Online (Sandbox Code Playgroud) struct some_struct{
some_struct *pointer_to_another_struct;
int a;
some_struct(int value ) :a (value), pointer_to_another(NULL)()
};
...
vector<some_struct *> tree;
...
for ( auto it = tree.begin(); it != tree.end(); ++it) {
it->a=10;
}
Run Code Online (Sandbox Code Playgroud)
这显然不是迭代结构指针向量的正确方法,因为我的编译器给了我错误.我该如何解决?另外,当我将指针存储在向量中时,有什么我应该担心的吗?
这与我之前的一个问题有关.我尝试了一点点扩展代码,并尝试使用不同的方法返回指向本地静态变量的指针,尤其是返回二维数组.(这只是为了理解数组的指针是如何工作的以及它们在函数环境中的行为方式.它甚至没有使用structs 以智能方式返回数组的最轻微的意图.)我编写了两个转置矩阵的函数.该函数transpose()仅转换2x4矩阵,该函数transpose_generic()应该转置任意大小的矩阵.他们没有真正的用处.在我声明并定义了我称之为的函数之后int main(int argc, char *argv[]){}.已经转置的两个矩阵应该以stdout简单的for循环打印.这是我不明白的事情.根据我是否声明应该转换为全局变量或局部变量的矩阵,我得到段错误或"干净"退出.我还可以通过在打印转置矩阵的for循环之后使用for循环来阻止编译程序的segfaulting stdout.但for循环需要使用新索引.我敢肯定我错过了一些非常基本的东西,但我无法自己解决这个问题.即使经过广泛的互联网搜索,我仍然感到困惑.所以也许有人可以开导我.这是代码:
#include <stdio.h>
int mat1[2][4] = {
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int mat2[2][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8}
};
typedef matx[2];
matx *transpose(int matrix[][4]);
typedef maty[];
maty *transpose_generic(int nrow, int ncol, int matrix1[nrow][ncol], int matrix2[nrow][ncol]);
int main(int argc, char *argv[]) {
/* This may need a little bit …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(){
const int a = 10;
*(int*)(&a) = 9; // modify a
printf("%d", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么?
我有两个结构如下:
typdef struct abc
{
int id;
char name;
}s_abc,*lpabc;
typdef struct result
{
int acc_no;
lpabc *details;
}s_res;
Run Code Online (Sandbox Code Playgroud)
我需要在结构结果中动态添加数据,这些数据指向指针数组,即:struct abc
结构abc可以是例如5个值的数组.我该如何添加值?
定义的结构是明确的:为了更好地理解我附加下面的结构: -
typedef struct _wfs_cdm_physicalcu
{
LPSTR lpPhysicalPositionName;
CHAR cUnitID[5];
ULONG ulInitialCount;
ULONG ulCount;
ULONG ulRejectCount;
ULONG ulMaximum;
USHORT usPStatus;
BOOL bHardwareSensor;
} WFSCDMPHCU, *LPWFSCDMPHCU;
typedef struct _wfs_cdm_cashunit
{
USHORT usNumber;
USHORT usType;
LPSTR lpszCashUnitName;
CHAR cUnitID[5];
CHAR cCurrencyID[3];
BOOL bAppLock;
USHORT usStatus;
USHORT usNumPhysicalCUs;
LPWFSCDMPHCU *lppPhysical;
} WFSCDMCASHUNIT, *LPWFSCDMCASHUNIT;
typedef struct _wfs_cdm_cu_info
{
USHORT …Run Code Online (Sandbox Code Playgroud) 我正在学习C并且有一些问题.我正在试图为每个声明的变量打印内存插槽,但是当我为Char []声明指针时,它只是不起作用.
这是我的代码:
int main () {
char a[3]; // this variable is my problem
int b;
float c;
char d;
int e=4;
char *pachar; //A char type variable for the pointer.
int *paint;
float *pafloat;
char *pacharr;
int *paintt;
pachar = &a; // when I try to assign the memory to the pointer, it shows a Warning message.
paint = &b;
pafloat = &c;
pacharr = &d;
paintt = &e;
printf("%p \n",pachar);
printf("%p \n",paint);
printf("%p \n",pafloat);
printf("%p \n",pacharr);
printf("%p …Run Code Online (Sandbox Code Playgroud) 我不明白指针如何指定char数组或字符串:
char* Carr = new char[5];
Carr = "Hello";
Run Code Online (Sandbox Code Playgroud)
但是下面的代码不起作用:
int* Iarr = new int[5];
Iarr = { 1,2,3,4 };
Run Code Online (Sandbox Code Playgroud)
我知道指针可以赋值而不是值,所以编译器如何接受字符串?