【发布时间】:2013-12-03 01:59:14
【问题描述】:
我正在学习 C 语言课程,刚刚开始学习指针。我刚去完成本节的实验室,但我的代码无法正常运行。于是我打开了实验室的正确答案代码,然后我一行一行的把实验室的答案代码和我自己的答案代码进行了比较。我的一些标签是不同的,但不会导致任何错误。
我不知道为什么我的代码不能运行,但实验室的代码可以。
这是我尝试运行代码时在 xcode 中发生的情况的图片:
非常感谢任何帮助。
这是我的代码不起作用:
#include <stdio.h>
int main()
{
int age = 40;
float gpa = 3.25;
char grade = 'A';
double x = 0.000009;
char companyName[20];
printf("The address of age is: %d\n", &age);
printf("The size of age is: %lu\n", sizeof(age));
printf("The address of gpa is: %d\n", &gpa);
printf("The size of gpa is: %lu\n", sizeof(gpa));
printf("The address of grade is: %d\n", &grade);
printf("The size of grade is: %lu\n", sizeof(grade));
printf("The address of x is: %d\n", &x);
printf("The size of x is: %lu\n", sizeof(x));
printf("The address of companyName is: %d\n", &companyName);
printf("The size of companyName is: %lu\n", sizeof(companyName));
int *pointerIntAge;
pointerIntAge = &age;
float *pointerFloatGpa;
pointerFloatGpa = &gpa;
char *pointerCharGrade;
pointerCharGrade = &grade;
double *pointerDoubleX;
pointerDoubleX = &x;
char *pointerCharCompanyName;
pointerCharCompanyName = &companyName;
printf("The value of pointerIntAge is: %d\n", *pointerIntAge);
printf("The value of pointerFloatGpa is: %f\n", *pointerFloatGpa);
printf("The value of pointerCharGrade is: %c\n", *pointerCharGrade);
printf("The value of pointerDoubleX is: %f\n", *pointerDoubleX);
printf("The valie of pointerCharCompanyName is: %s\n", *pointerCharCompanyName);
printf("The address of pointerIntAge is: %p\n", pointerIntAge);
printf("The address of pointerFloatGpa is: %p\n", pointerFloatGpa);
printf("The address of pointerCharGrade is: %p\n", pointerCharGrade);
printf("The address of pointer DoubleX is: %p\n", pointerDoubleX);
printf("The address of pointerCharCompanyName is: %p\n", pointerCharCompanyName);
*pointerIntAge+=5;
printf("Just added 5 to pointer age\n");
printf("The new value of age is: %d\n", age);
printf("The value of age through pointer is: %d\n", *pointerIntAge);
return 0;
}
这是实验室的有效代码:
#include <stdio.h>
int main ()
{
int age = 40;
float gpa = 3.25;
char grade = 'A';
double x = 0.000009;
char companyName[20];
printf("Address of age: %d\n", &age);
printf("Size of age: %lu\n", sizeof(age));
printf("Address of GPA: %d\n", &gpa);
printf("Size of age: %lu\n", sizeof(gpa));
printf("Address of grade: %d\n", &grade);
printf("Size of grade: %lu\n", sizeof(grade));
printf("Address of x: %d\n", &x);
printf("Size of x: %lu\n", sizeof(x));
printf("Address of companyName: %d\n", &companyName);
printf("Size of companyName: %lu\n", sizeof(companyName));
int *pAge;
pAge = &age;
float *pGpa;
pGpa = &gpa;
char *pGrade;
pGrade = &grade;
double *pX;
pX = &x;
char *pCompanyName;
pCompanyName = &companyName;
printf("\nValue of Age though pointer: %d", *pAge);
printf("\nValue of GPA though pointer: %0.2f", *pGpa);
printf("\nValue of Grade though pointer: %c", *pGrade);
printf("\nValue of X though pointer: %f", *pX);
printf("\nValue of Company Name though pointer: %s\n", *pCompanyName);
printf("\nThe address from Age pointer: %p", pAge);
printf("\nThe address from Gpa pointer: %p", pGpa);
printf("\nThe address from Grade pointer: %p", pGrade);
printf("\nThe address from X pointer: %p", pX);
printf("\nThe address from companyName pointer: %p\n", pCompanyName);
*pAge += 5; //Added 5 to Age through pointer.
printf("\nValue of Age: %d", age);
printf("\nValue of Age through pointer: %d", *pAge);
return 0;
}
【问题讨论】:
-
您遇到了什么错误? 您的代码(不是实验室的)的实际输出是什么?
-
@Jonah 我刚刚更新了我的原始帖子,并附上了我在尝试运行时在 xcode 中得到的截图。
标签: c pointers sizeof memory-address