Code not running when trying to display pointer values and pointer addresses in C

mpbiz

New member
Apr 29, 2010
2,825
57
0
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:
nL76h.png


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;
}
 


Try changing:

Code:
*pointerIntAge+=5;

To:

Code:
*pointerIntAge += 5;

Add some spaces. Does that work?
 
Try changing:

Code:
*pointerIntAge+=5;

To:

Code:
*pointerIntAge += 5;

Add some spaces. Does that work?

Nope.

Multiple people have told me the code is flawed anyways. Figured someone on here still might be able to help. Just need to find some more lessons on pointers.

Thanks for the help.
 
Hmmm... maybe try changing "pointer" to "p". So instead of having "pointerIntAge", change it to "pIntAge". And maybe even try taking the data types out of the variable names (eg. int, char, etc.).

Not sure how the gcc compiler works in detail, but it might be getting pissy because you have reserved words like "pointer" or "int" within your variable names.
 
Using gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1), both code compiles and runs, but with warnings.

Supplied code warnings:
Code:
corrrect.c: In function ‘main’:
corrrect.c:11:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
corrrect.c:13:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘float *’ [-Wformat]
corrrect.c:15:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
corrrect.c:17:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double *’ [-Wformat]
corrrect.c:19:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
corrrect.c:31:18: warning: assignment from incompatible pointer type [enabled by default]
corrrect.c:37:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

And output:
Code:
Address of age: -1177522504
Size of age: 4
Address of GPA: -1177522500
Size of age: 4
Address of grade: -1177522505
Size of grade: 1
Address of x: -1177522496
Size of x: 8
Address of companyName: -1177522448
Size of companyName: 20

Value of Age though pointer: 40
Value of GPA though pointer: 3.25
Value of Grade though pointer: A
Value of X though pointer: 0.000009
Value of Company Name though pointer: (null)

The address from Age pointer: 0x7fffb9d06eb8
The address from Gpa pointer: 0x7fffb9d06ebc
The address from Grade pointer: 0x7fffb9d06eb7
The address from X pointer: 0x7fffb9d06ec0
The address from companyName pointer: 0x7fffb9d06ef0

Value of Age: 45

Your code warnings:

Code:
mpbiz.c: In function ‘main’:
mpbiz.c:13:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
mpbiz.c:16:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘float *’ [-Wformat]
mpbiz.c:19:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
mpbiz.c:22:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double *’ [-Wformat]
mpbiz.c:25:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
mpbiz.c:46:28: warning: assignment from incompatible pointer type [enabled by default]
mpbiz.c:53:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

And output:

Code:
The address of age is: -426145032
The size of age is: 4
The address of gpa is: -426145028
The size of gpa is: 4
The address of grade is: -426145033
The size of grade is: 1
The address of x is: -426145024
The size of x is: 8
The address of companyName is: -426144976
The size of companyName is: 20
The value of pointerIntAge is: 40
The value of pointerFloatGpa is: 3.250000
The value of pointerCharGrade is: A
The value of pointerDoubleX is: 0.000009
The valie of pointerCharCompanyName is: (null)
The address of pointerIntAge is: 0x7fffe6998af8
The address of pointerFloatGpa is: 0x7fffe6998afc
The address of pointerCharGrade is: 0x7fffe6998af7
The address of pointer DoubleX is: 0x7fffe6998b00
The address of pointerCharCompanyName is: 0x7fffe6998b30
Just added 5 to pointer age
The new value of age is: 45
The value of age through pointer is: 45

This is just running "gcc file.c -o file; ./file"

I doubt gcc cares about what variables are called.
 
You'll always get warnings if you try and use format specifiers like %d, %s, etc, to print a pointer address. The only valid ANSI C printf specifier for a pointer is %p. In other words, the book example is flat out wrong. It's also making all sorts of assumptions, like, for example, that a pointer is the same size as an int. Not (necessarily) true, especially on a 64 bit machine.

Also, you never assign anything to char companyName, so it won't ever print a sensible value.

Here's your code, fixed such that it compiles with "gcc -Wall" on a linux box without emitting any warnings.

Tell your professor the example sucks.
Code:
#include <stdio.h>

int main()
{

    int age = 40;
    float gpa = 3.25;
    char grade = 'A';
    double x = 0.000009;
    /* length of the string should match the '20' */
    char companyName[20]="need some name here.";

    printf("The address of age is: %p\n",&age);
    printf("The size of age is: %lu\n", sizeof(age));

    printf("The address of gpa is: %p\n",&gpa);
    printf("The size of gpa is: %lu\n", sizeof(gpa));

    printf("The address of grade is: %p\n",&grade);
    printf("The size of grade is: %lu\n", sizeof(grade));

    printf("The address of x is: %p\n",&x);
    printf("The size of x is: %lu\n", sizeof(x));

    printf("The address of companyName is: %p\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 = (char *)&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 value of pointerCharCompanyName is: %s\n",(char *)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;
}
 
^^^ Oops.

FYI, my value for companyName above has one too many chars in it...needs room for the trailing null. Remove the period at the end. Or, change to char companyName[21], or better yet, char companyName[].
 
This may not be doing what you think it is doing:
pCompanyName = &companyName;
You have declared companyName as a char [20]

If you want the address of the first element, it is companyName, or &companyName[0]

If you give just &companyName, it is looking for a (likely) 4 byte address at a 1-byte location, or it treats the first 4 elements of the array as an address
 
Why are you learning C as your first language?

i agree, don't think you should be learning C at all. i think the productive uses of C are limited these days. it's just too low level when computers are so powerful. and anyway, you'll only be learning the basics of C so you're not going to be doing much with it. and objective-c is very different to C imo. once you grok the concept of pointers, job done, move on.

from your PM, you want to make iOS apps. that's good that you've an endgoal as I HATE the way of learning programming where teachers make you code stupid things like guess the number. having an endgoal and just figuring it out is my favourite way of learning and also the most productive.

i say just download the Big Nerd ranch guide to iOS programming and read through the first few chapters to get a grounding. i think the first 2 are essential to get a base on Objective-C. then the last part of chapter 3 to understand dot syntax as it's nearly always used in real-life code and really confused me at the start. then chapter 7 to understand view controllers and the MVC stuff that cocoa loves.

after you've read through that, crank up xcode and get started on some app you want to make! when i'm learning, i like to download a sample app that's similiar to the one i want to make and edit it. much less daunting that starting with a blank slate.

of course, you're going to be slow at the start and you'll be constantly googling concepts of objective-C to understand things. the big nerd book doesn't mess about, every sentence needs to be understood and absorbed. i found myself constantly going back and re-reading things. post threads here if you're stuck.

i'm not a great coder but i've a few apps in iTunes store and more in Android. iOS is way harder than Android IMO. and they're all harder than PHP. i think IMers who've done a bit of PHP get a serious shock when they try and do iPhone apps. but just stick with it. there's a serious amount of money in the iOS/Android markets as you all know. i'm making my living on it. you wouldn't believe the stupid apps that people pay for. best thing is the difficulty of programming is a good barrier of entry to keep people out.
 
This may not be doing what you think it is doing:

You have declared companyName as a char [20]

If you want the address of the first element, it is companyName, or &companyName[0]

If you give just &companyName, it is looking for a (likely) 4 byte address at a 1-byte location, or it treats the first 4 elements of the array as an address

Nope. &companyName and &companyName[0] are the same thing....the starting memory address. companyName, in this case, isn't a pointer, it's an array of chars.
 
Thanks for all of the help so far everyone.

@harry,

Thanks for the advice. I'm going to spend some more time on C, and really want to finish strong on the C course I've been taking just so I can say that I finished it. I'm on the last section anyways, and then I'll follow your advice and go through the big nerd ranch ebook while I go through this same instructor's objective-C course as well.

I laughed when you said "from your PM, you want to make iOS apps. that's good that you've an endgoal as I HATE the way of learning programming where teachers make you code stupid things like guess the number. having an endgoal and just figuring it out is my favourite way of learning and also the most productive.

This is so true. I always had a terrible time in school because all the teacher does is talk. I need to be doing something constantly and so far this is working really well. I type along with the videos and then dig into the labs right away and just keep coming back to them and repeating, adding new things, etc.

Another thing that has really been helping me is to translate out the different bits of code into plain english. I have always been good at reading and writing but suck at math and code has always scared me because it looks like math.

I have been translating the code to plain english and the concepts are really sticking with me by doing this. It's really funny if you think about it, most people probably don't sit down and think about code first in their head, or at least not me.

For me, I have an app idea I want to create, and in my head is English like "I want the app to do blah blah blah" so if I can just keep translating the code to plain english I think I'll be just fine. Going to make some flash cards with code to english translations.

Anyways, thanks to everyone for the help so far and I'm really excited to keep moving forward with this stuff.

I do have a new problem that I could use some help with. I am trying to translate this code to english:

Code:
char sentence[47] = "The quick brown fox jumped over the lazy dogs.";
  
    char *posA = strrchr(sentence, 'a');
    printf("\nThe last occurance of 'a': %ld", posA-sentence);


So far here is what I have it translated to:

strrchr means "Character to be located in the string."

So in the above code when you see "strrchr(sentence, 'a');"

It is saying "character to be located in string. Search in the sentence string and find the character 'a'.

I also understand that posA-sentence is just telling the computer what we want to print for the value %ld, but what's confusing me is the "%ld" and the fact that it says we are locating the last 'a' in the entire sentence.

What does %ld mean in this situation?

Also, what about "*posA" Does *pos mean a new type of pointer or does it mean position? Because I thought pointer was just *pointer

Thanks for the help.
 
strrchr (string-reverse-char) finds the last occurrence, where strchr would find the first one.

%ld means long int. I am guessing that they made it a long int in case the text to be searched was massive.

You will see lots of weird pointer things done with char arrays that you won't see with other types. Generally, when you are working with a pointer to a char array element, consider it the 0-based position in a string.
 
Nope. &companyName and &companyName[0] are the same thing....the starting memory address. companyName, in this case, isn't a pointer, it's an array of chars.

Ah, when I am messing with strings in C, I sometimes forget the differences between [] and * notations in a declaration.

Since companyName is never set in the examples, it is masking a problem:
Code:
printf("The valie of pointerCharCompanyName is: %s\n", *pointerCharCompanyName);
Since char[20] declaration 'should' make 20 null bytes, the printf statement will produce an empty string, or (null), depending on how the pointer was set.

If companyName is actually set to something, like:
Code:
int n = sprintf(companyName, "Google Hammer");
dereferencing the pointerCharCompanyName in the printf statament will cause a seg fault.

Technically though, if you set a companyName and remove the * in the printf, they will all 3 work in the first example in the OP.

We now have n instead of sizeof(companyName) to get the length.


The following variations work when printing pointerCharCompanyName, and all 3 will seg fault when printing *pointerCharCompanyName:

Code:
pointerCharCompanyName = companyName;
pointerCharCompanyName = (char*)&companyName; // will cause compile warning if not explicitly cast
pointerCharCompanyName = &companyName[0]
 
Like I said earlier thanks for all of the help everyone.

I just had a major breakthrough and posted about it here.

I look forward to spamming your wf inboxes with coding questions bros.