我正在参加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;
}
答案 0 :(得分:1)
companyName
未初始化,因此printf("The valie of pointerCharCompanyName is: %s\n", *pointerCharCompanyName);
将打印垃圾(如果其中某处存在NULL)或超出范围(如果没有)。
这是因为C中的字符串(实际上只是字符数组)应该是以null结尾的。这意味着它们的末尾应该有一个'\ 0'字符 - 操作字符串的方法使用它来知道字符串的结束位置。
尝试char companyName[20] = { 0 };
或char companyName[20] = { "some string" };
但还有一些其他问题需要解决。
%p
打印地址,而不是%d
。pointerCharCompanyName = &companyName;
和printf("The valie of pointerCharCompanyName is: %s\n", *pointerCharCompanyName);
都会因对指针理解不当而导致问题。答案 1 :(得分:0)
您的问题就在这一行:pCompanyName = &companyName;
您需要打开编译器警告。你应该给gotton一个警告“从不兼容的指针类型中分配。”
companyName已经是一个指针。因此,您的行尝试分配指向指针地址的指针(即指向指针的指针)。
应该是:pCompanyName = &companyName[0];
或只是pCompanyName = companyName;
此外,您在printf("The address of age is: %d\n", &age);
此外,两个版本的代码都错误地使用%lu而不是%u来表示sizeof(返回size_t,它是unsigned int,而不是unsigned long)。
实验室代码也是错误的。如果它崩溃是基于如何分配内存的愚蠢运气。您还有使用%s打印没有值的字符串的问题,因此将随机地可能有或没有null终止符。您还需要将带有null终止符的字符放入公司名称中。像companyName[20] = "test";