C 中的指针 int,需要帮助理解和简化代码片段
pointer int in C, need help understanding and simplifying code snippet
我刚刚写了这段代码,当它扫描整数时,已经将整数值传递给 for,但是我在最后返回了 int 的内存地址。我如何只显示我只是读入而不是它的地址?我可以进一步简化此代码段吗?
#include <stdlib.h>
#include <stdio.h>
typedef struct building {
char *blockName;
int blockNumber;
} building;
int main() {
building *blockA = (building*)malloc(sizeof(building));
building *blockB = (building*)malloc(sizeof(building));
blockA->blockName = (char*)malloc(25*sizeof(char*));
blockB->blockName = (char*)malloc(25*sizeof(char*));
blockA->blockNumber = (int)malloc(sizeof(int));
blockB->blockNumber = (int)malloc(sizeof(int));
printf("What is the name for your first block: ");
scanf("%s", (*blockA).blockName);
printf("What will be it's number: ");
scanf("%d", (*blockA).blockNumber);
printf("\n");
printf("What is the name for your second block: ");
scanf("%s", (*blockB).blockName);
printf("What will be it's number: ");
scanf("%d", (*blockB).blockNumber);
printf("\n");
printf("Your first block's name is %s. It's number is %d\n", (*blockA).blockName, (*blockA).blockNumber);
printf("Your second block's name is %s. It's number is %d\n", (*blockB).blockName, (*blockB).blockNumber);
printf("\n");
free(blockA->blockName);
free(blockB->blockName);
free(blockA);
free(blockB);
system("pause");
return 0;
}
会员
int blocknumber;
是普通标量整数,不是指针。直接使用,不分配内存。这一行:
blockA->blockNumber = (int)malloc(sizeof(int));
非常可疑,您的编译器应该警告过您。 (您确实在启用警告的情况下进行编译,不是吗?)您正在尝试将指针存储在整数值中,这将在指针大小大于 int 大小的机器上失败。
补救的办法就是不给标量分配内存,那么
scanf("%d", &(*blockB).blockNumber);
(注意 &
),然后您将获得可用的用户输入:
printf("It's number is %d\n", (*blockB).blockNumber);
另一方面,字符串的 malloc
是正确的,因为字符串是一个字符数组,在本例中分配在堆上并由指向第一个字符的指针表示。
由于建筑被定义为
typedef struct building {
char *blockName;
int blockNumber;
} building;
你不应该这样做
blockA->blockNumber = (int)malloc(sizeof(int));
因为行 building *blockA = (building*)malloc(sizeof(building));
已经在 int blockNumber
的堆上分配了 space:
你可以简单地分配它
blockA->blockNumber = 1234;
或提示用户输入
scanf("%d", &(blockA->blockNumber));
我刚刚写了这段代码,当它扫描整数时,已经将整数值传递给 for,但是我在最后返回了 int 的内存地址。我如何只显示我只是读入而不是它的地址?我可以进一步简化此代码段吗?
#include <stdlib.h>
#include <stdio.h>
typedef struct building {
char *blockName;
int blockNumber;
} building;
int main() {
building *blockA = (building*)malloc(sizeof(building));
building *blockB = (building*)malloc(sizeof(building));
blockA->blockName = (char*)malloc(25*sizeof(char*));
blockB->blockName = (char*)malloc(25*sizeof(char*));
blockA->blockNumber = (int)malloc(sizeof(int));
blockB->blockNumber = (int)malloc(sizeof(int));
printf("What is the name for your first block: ");
scanf("%s", (*blockA).blockName);
printf("What will be it's number: ");
scanf("%d", (*blockA).blockNumber);
printf("\n");
printf("What is the name for your second block: ");
scanf("%s", (*blockB).blockName);
printf("What will be it's number: ");
scanf("%d", (*blockB).blockNumber);
printf("\n");
printf("Your first block's name is %s. It's number is %d\n", (*blockA).blockName, (*blockA).blockNumber);
printf("Your second block's name is %s. It's number is %d\n", (*blockB).blockName, (*blockB).blockNumber);
printf("\n");
free(blockA->blockName);
free(blockB->blockName);
free(blockA);
free(blockB);
system("pause");
return 0;
}
会员
int blocknumber;
是普通标量整数,不是指针。直接使用,不分配内存。这一行:
blockA->blockNumber = (int)malloc(sizeof(int));
非常可疑,您的编译器应该警告过您。 (您确实在启用警告的情况下进行编译,不是吗?)您正在尝试将指针存储在整数值中,这将在指针大小大于 int 大小的机器上失败。
补救的办法就是不给标量分配内存,那么
scanf("%d", &(*blockB).blockNumber);
(注意 &
),然后您将获得可用的用户输入:
printf("It's number is %d\n", (*blockB).blockNumber);
另一方面,字符串的 malloc
是正确的,因为字符串是一个字符数组,在本例中分配在堆上并由指向第一个字符的指针表示。
由于建筑被定义为
typedef struct building {
char *blockName;
int blockNumber;
} building;
你不应该这样做
blockA->blockNumber = (int)malloc(sizeof(int));
因为行 building *blockA = (building*)malloc(sizeof(building));
已经在 int blockNumber
的堆上分配了 space:
你可以简单地分配它
blockA->blockNumber = 1234;
或提示用户输入
scanf("%d", &(blockA->blockNumber));