Can't pass chars with pointers?


#1

I maybe a quick program as practice for myself for passing arguments/parameters and using pointers… I realize the function isn’t really necessary but pretend it’s a lot more complex than it actually is. Anyway, I get errors and the errors don’t make sense to me why they are error.

It gives me an error on the printf line in main() that says:
format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
…but argument 2 is colorOfEgg which I had declared as char, not int

also, on these 3 lines it says:
assignment makes integer from pointer without a cast

*color = “white.”;
*color = “brown.”;
*color = “unknown.”;
color was declared as a char … why would a cast be needed at all? colorOfEgg and color are both chars, I’m not converting anything via cast

[code]#include <stdio.h>

void eggs (int a, char *color) {

if (a == 1) {
	*color = "white";
} else if (a == 2) {
	*color = "brown";
} else {
	*color = "unknown";
}

}

int main (int argc, const char * argv[]) {

char colorOfEgg;

eggs(2, &colorOfEgg); // change 2 to 1, 2, or another int to get different results of the eggs color
printf("The color of the egg is %s",colorOfEgg);

return 0;

}[/code]


#2

When you dereference the pointer (*color = ) it expects a char, not a char *.

Leaving out the dereference will get rid of the error:

but it won’t do what you want. To understand why, you must get to the “Pass by reference chapter”.


#3

Change: void eggs (int a, char *color) to: void eggs (int a, const char **color)
Change: char colorOfEgg; to: const char *colorOfEgg;

And try again.

#include <stdio.h>

void eggs (int a, const char **color) {

if (a == 1) {
*color = “white”;
} else if (a == 2) {
*color = “brown”;
} else {
*color = “unknown”;
}

}

int main (int argc, const char * argv[]) {

const char *colorOfEgg;

eggs (2, &colorOfEgg); // change 2 to 1, 2, or another int to get different results of the eggs color
printf(“The color of the egg is %s\n”,colorOfEgg);

return 0;

}


#4

Can you explain why my code doesn’t work? I even copied the format from the Pass By Reference section (pg 63). I don’t get it.

void getColor(int a, char *color)
{
    char finalColor;
    if (a == 1) {
        finalColor = "White";
    } else if (a == 2) {
        finalColor = "Brown";
    } else {
        finalColor = "Questionable";
    }
    *color = finalColor;
}

int main(int argc, const char * argv[])
{
    int a = 1;
    char color;
    getColor(a, &color);
    printf("the color of the egg is %s", color);
    return 0;
}

#5

Your code is still riddled with problems; you need to work on your understanding of pointers.

void getColor (int a, char *color)
// this should be: void getColor (int a, const char **color)
// because you want to return the address of a sequence of constant characters in color
 // back to the invoker
{
    char finalColor;
     // this should be: const char *finalColor;
     // because you want to store the address of a sequence of constant characters  in it

    if (a == 1) {
        finalColor = "White";
    } else if (a == 2) {
        finalColor = "Brown";
    } else {
        finalColor = "Questionable";
    }
    *color = finalColor;
}

int main (int argc, const char * argv[])
{
    int a = 1;

    char color;
    // this should be: const char *color
    // because your intention is to have getColor () to store and return in color a pointer to a sequence of constant characters

    getColor (a, &color);
    printf ("the color of the egg is %s", color);
    return 0;
}

Here is the revised (and correct) version:

#include <stdio.h>

void getColor (int a, const char **color)
{
    const char *finalColor;
    if (a == 1) {
        finalColor = "White";
    } else if (a == 2) {
        finalColor = "Brown";
    } else {
        finalColor = "Questionable";
    }
    *color = finalColor;
}

int main (int argc, const char * argv[])
{
    int a = 1;
    const char *color;
    getColor (a, &color);
    printf ("the color of the egg is %s\n", color);
    return 0;
}

Given the following function declarations, make sure that you understand the differences between them in terms what effect the functions can have when invoked on their argument objects.

typedef T;

void Mercury (T);
void Venus (T*);
void Earth (T**);
void Mars (const T);
void Jupiter (const T*);
void Saturn (const T**);


#6

I don’t know if you have the book, but if you do, check out page 63. How is that code any different from mine?

You’ll notice that I use the same style. Why can I declare double *thetaPtr here, but in my previous code I need const char **color (i.e. why do I need the ** instead of just *)? Also, when I set my char finalColor variable, why do I need const char *finalColor, when the code below doesn’t use a pointer?

Here’s what’s on page 63:

#include <stdio.h>
#include <math.h>

void cartesianToPolar(float x, float y, double *rPtr, double *thetaPtr)
{
     // Store the radius in the supplied address
     *rPtr = sqrt(x * x + y * y);
 
    // Calculate theta
    float theta;
    if (x == 0.0) {
        if (y == 0) {
            theta = 0.0; //technically undefined
        } else if (y > 0) {
            theta = M_PI_2;
        } else {
            theta = - M_PI_2;
        }
    } else {
        theta = atan(y/x);
    }
    // Store theta in the supplied address
    *thetaPtr = theta;
}

int main(int argc, const char * argv[])
{
    double x = 3.0;
    double y = 4.0;
    double radius;
    double angle;
    
    cartesianToPolar(x, y, &radius, &angle);
    printf("(%.2f, %.2f) becomes (%.2f radians, %.2f)\n", x, y, radius, angle);
    
    return 0;
}

#7

The distinction lies between returning a double (a scalar object) and returning the address of a sequence (vector) of constant characters.

You should read the following book:

"The C Programming Language, Brian W. Kernighan and Dennis M. Ritchie."
Make sure that you firmly understand:

  • functions;
  • structs;
  • unions;
  • sizeof operator;
  • arrays;
  • malloc (), free (), memcpy (), strcpy (), printf, sprintf, etc.
  • pointers (of all kinds);
  • null-terminated char arrays.

And tackle the Objective-C afterwards.


#8

I have the Objective-C book, true; however, the first 11 chapters are nothing but C.

With that said, the book doesn’t mention anything about pointers of pointers etc. I’ve done a bit of googling and I can’t find an answer that is really clicking with me.

Will you please shed a little bit of light?

Thanks.