I am taking a course in C and just started learning about pointers.
I just went to go complete this section's lab and I couldn't get my code to run properly.
So I opened up the lab's correct answer code, and then I went through line by line and compared the lab's answer code to my own.
Some of my label's are different, but nothing that should be causing any errors.
I can't figure out why my code won't run, but the lab's code will.
Here is a picture of what happens in xcode when I try to run my code:
I already asked the stackoverflow gods for help and someone said that the "correct" lab code is just as shitty as my code, but like I said the "correct" lab code will run but mine will not.
I have a feeling the instructor of this course rushed through it because I have encountered multiple speed bumps in other sections as well, but this one is really giving me a hard time and I can't seem to figure it out.
I pasted the 2 sets of codes below, but if it's harder to read on here then here's a link to the stackoverflow post I made: c - Code not running when trying to display pointer values and pointer addresses - Stack Overflow
Here is my code that won't run (See screenshot above of what happens when I try to run it) :
Here is the lab's "correct" answer code:
I just went to go complete this section's lab and I couldn't get my code to run properly.
So I opened up the lab's correct answer code, and then I went through line by line and compared the lab's answer code to my own.
Some of my label's are different, but nothing that should be causing any errors.
I can't figure out why my code won't run, but the lab's code will.
Here is a picture of what happens in xcode when I try to run my code:
I already asked the stackoverflow gods for help and someone said that the "correct" lab code is just as shitty as my code, but like I said the "correct" lab code will run but mine will not.
I have a feeling the instructor of this course rushed through it because I have encountered multiple speed bumps in other sections as well, but this one is really giving me a hard time and I can't seem to figure it out.
I pasted the 2 sets of codes below, but if it's harder to read on here then here's a link to the stackoverflow post I made: c - Code not running when trying to display pointer values and pointer addresses - Stack Overflow
Here is my code that won't run (See screenshot above of what happens when I try to run it) :
Code:
#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;
}
Here is the lab's "correct" answer code:
Code:
#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;
}