Note that there are some explanatory texts on larger screens.

plurals
  1. POCode not running when trying to display pointer values and pointer addresses
    primarykey
    data
    text
    <p>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.</p> <p>I can't figure out why my code won't run, but the lab's code will. </p> <p>Here is a picture of what happens in xcode when I try to run my code:</p> <p><img src="https://i.stack.imgur.com/nL76h.png" alt="enter image description here"></p> <p>Any help is greatly appreciated.</p> <p>Here is my code that is not working:</p> <pre><code>#include &lt;stdio.h&gt; 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", &amp;age); printf("The size of age is: %lu\n", sizeof(age)); printf("The address of gpa is: %d\n", &amp;gpa); printf("The size of gpa is: %lu\n", sizeof(gpa)); printf("The address of grade is: %d\n", &amp;grade); printf("The size of grade is: %lu\n", sizeof(grade)); printf("The address of x is: %d\n", &amp;x); printf("The size of x is: %lu\n", sizeof(x)); printf("The address of companyName is: %d\n", &amp;companyName); printf("The size of companyName is: %lu\n", sizeof(companyName)); int *pointerIntAge; pointerIntAge = &amp;age; float *pointerFloatGpa; pointerFloatGpa = &amp;gpa; char *pointerCharGrade; pointerCharGrade = &amp;grade; double *pointerDoubleX; pointerDoubleX = &amp;x; char *pointerCharCompanyName; pointerCharCompanyName = &amp;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; } </code></pre> <p>Here is the lab's code that is working:</p> <pre><code>#include &lt;stdio.h&gt; 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", &amp;age); printf("Size of age: %lu\n", sizeof(age)); printf("Address of GPA: %d\n", &amp;gpa); printf("Size of age: %lu\n", sizeof(gpa)); printf("Address of grade: %d\n", &amp;grade); printf("Size of grade: %lu\n", sizeof(grade)); printf("Address of x: %d\n", &amp;x); printf("Size of x: %lu\n", sizeof(x)); printf("Address of companyName: %d\n", &amp;companyName); printf("Size of companyName: %lu\n", sizeof(companyName)); int *pAge; pAge = &amp;age; float *pGpa; pGpa = &amp;gpa; char *pGrade; pGrade = &amp;grade; double *pX; pX = &amp;x; char *pCompanyName; pCompanyName = &amp;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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload