指针的大小是否与其指向的类型的大小相同,或者指针总是具有固定大小?例如...
int x = 10;
int * xPtr = &x;
char y = 'a';
char * yPtr = &y;
std::cout << sizeof(x) << "\n";
std::cout << sizeof(xPtr) << "\n";
std::cout << sizeof(y) << "\n";
std::cout << sizeof(yPtr) << "\n";
Run Code Online (Sandbox Code Playgroud)
这会是什么输出?会sizeof(xPtr)返回4并sizeof(yPtr)返回1,或者2指针实际上会返回相同的大小吗?我问这个的原因是因为指针存储的是存储器地址而不是它们各自存储的地址的值.
我正在从一个文件中读取数字.当我尝试将每个数字放入一个双维数组时,它给出了我的错误.我如何摆脱这个消息?我的变量:FILE*fp; char line [80];
错误:从char*转换为int会失去精度
码:-
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char line[80],*pch;
int points[1000][10];
int centroid[1000][10];
float distance[1000][10];
int noofpts=0,noofvar=0,noofcentroids=0;
int i=0,j=0,k;
fp=fopen("kmeans.dat","r");
while(fgets(line,80,fp)!=NULL)
{
j=0;
pch=strtok(line,",");
while(pch!=NULL)
{
points[i][j]=(int)pch;
pch=strtok(NULL,",");
noofvar++;
j++;
}
noofpts++;
i++;
}
noofvar=noofvar/noofpts;
printf("No of points-%d\n",noofpts);
printf("No of variables-%d\n",noofvar);
return 0;
}
Run Code Online (Sandbox Code Playgroud)