我有这段代码片段
const int col= 5;const int row= 5;
int a[row][col] = {0};
int (*p)[col] ;
p = a;
Run Code Online (Sandbox Code Playgroud)
这些语句打印相同的地址
cout <<p;
cout << endl;
cout << *p;
Run Code Online (Sandbox Code Playgroud)
在我看来,因为p指向一个5的数组ints,解引它
应该给出第一个似乎不是这样的值.
救命!
我有以下方法:
17 //returns size in bytes of binary file stream
18 //leaves the file at original position
19 long getFileSize(FILE * fp)
20 {
21 long pos = -1;
22 fpos_t *curPos;
23 if(fgetpos(fp, curPos) == 0)
24 {
25 fseek(fp, 0L, SEEK_END);
26 pos = ftell(fp);
27 fsetpos(fp, curPos);
28 }
29
30 return pos;
31 }
Run Code Online (Sandbox Code Playgroud)
这将编译和工作,但编译器会给我一个警告,因为curPos不是一个初始化变量.
如果我将该行更改为(编译器推荐)
22 fpos_t *curPos = NULL;
Run Code Online (Sandbox Code Playgroud)
我收到一个段错误.
为什么会这样?
我有一个小程序,在编译时会抛出以下错误
错误#2168:'+'的操作数具有不兼容类型'struct agenda'和'int'.
错误#2113:'.'的左操作数 具有不兼容的类型'int'.
错误#2088:需要左值.
这是我所做的代码
#include <stdio.h>
struct agenda{
int order, cellular;
char name[30], last_name[30], street[30], city[30], mail[50];
}contact[10];
int main(void)
{
struct agenda *ptrcontact;
ptrcontact = &contact[0];
(*ptrcontact+3).order = 3;
printf("\n\n %d", (*ptrcontact).order);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因为它会抛出这些错误以及如何修复它们?
如果控制返回到main()变量之后,如果从堆栈中删除了i仍然是5,因为我在main()中不存在并且指针指向的变量不是存在.
#include<stdio.h>
int* sum() {
int i=5;
int*a=&i;
printf("%d\n",a);
return a;
}
int main() {
int* a=sum();
printf("%d\n",a);
printf("%d",*a);
}
Run Code Online (Sandbox Code Playgroud)
输出:
2293252
2293252
5
Run Code Online (Sandbox Code Playgroud) 我正在研究的C++程序旨在通过利用无限数量的节点结构指针创建一个C++自定义双链接列表类,其中包含数据值和内部两个指针.这是该计划的前提.不能使用STL LinkedList类,必须自己创建.
这就是说,如何创建一个没有特定指针数量的类成员变量?我不想在声明类时初始化50个节点,但是只使用其中的10个.同时,我不想只初始化5,然后必须使用超过5.有一种方法可以在C++中动态地将节点指针添加到链表类,当节点指针被认为是成员变量时?
我是否正确地走这条路?如果是这样,我将如何做到这一点?
调试时我发现我的程序停在:"cout <<*ptr; cout <<"\n";" 这段代码有什么问题?
#include<iostream>
using namespace std;
int *ptr = 0;
void myfun(void);
int main()
{
void myfun();
for(int j = 1; j < 3; j++)
{
ptr = ptr-j ;
cout << *ptr ; cout << "\n";
}
return(0);
}
void myfun(void)
{
int x[3] = {11,12,13};
for(int i = 0; i <3; i++)
{
ptr = &x[i];
ptr = ptr+1;
}
}
Run Code Online (Sandbox Code Playgroud) 即使我写这个声明
char *test= new char[35];
Run Code Online (Sandbox Code Playgroud)
sizeof(test)将始终返回4(或另一个数字,具体取决于系统)而不是35.我认为这是因为指针的大小严格来说是物理"指向实体"而不是为该指针保留的内存量.这是对的吗?
此外,有没有办法使用sizeof()检索为特定指针保留的内存量?
我在C中有以下代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
typedef struct sample {
int num;
} abc;
typedef struct exmp{
abc *n1;
} ccc;
abc *foo;
foo = (abc*)malloc(sizeof(foo));
ccc tmp;
tmp.n1 = foo;
ccc stack[10];
stack[0] = tmp;
printf("address of tmp is %p\n",&tmp);
// need to print address contained in stack[0]
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我想检查地址at stack[0]是否与tmp 的地址相同.stack[0]当我打印出tmp的地址时,如何打印地址?
我不像C++那样熟悉C,但我发现自己需要调试C应用程序,甚至成功后,但我不确定我的修复是否确实正确.
这是一个代码:
FcValueList **value = NULL;
value = (FcValueList **) malloc (SIZEOF_VOID_P * nobjs);
// other code...
if (value)
free (value);
Run Code Online (Sandbox Code Playgroud)
所述FcValueList是一个结构和SIZEOF_VOID_P是尺寸void指针的.上面的代码不起作用,因为值 "无法读取内存"
所以我应用了以下修复程序,代码使用app退出0:
value = (FcValueList **) malloc (SIZEOF_VOID_P * nobjs);
// other code...
if (*value)
free (value);
Run Code Online (Sandbox Code Playgroud)
我的修正是否正确?如果不是什么会是正确的呢?
好吧所以我遇到了这种指针类型,我试图在我的代码中使用它,但我得到编译器的警告说它在不兼容的指针类型
这是类型
data_type (*i)[j]与i作为变量的名称和j作为指针的大小
例如,如果您希望指针的大小为4 ints,则需要声明
int (*i)[4] 然后你需要分配一个4个整数的数组
i = &s[4]
Run Code Online (Sandbox Code Playgroud)
但是当我尝试分配它时,我收到编译器发出的警告 incompatible type
那么这里的问题似乎是什么?以及如何正确使用它?