c programming string errror

MrChips

Joined Oct 2, 2009
29,848
You have two errors, line 177 and line 196.

177 change_the_colour(&colour);

should to be
change_the_colour(colour);

The reason is colour by itself is already the address of the array.

For the same reason,
196 *color = "blue";

should be
color = "blue";
 

Thread Starter

Alex1700

Joined Jan 12, 2020
106
You have two errors, line 177 and line 196.

177 change_the_colour(&colour);

should to be
change_the_colour(colour);

The reason is colour by itself is already the address of the array.

For the same reason,
196 *color = "blue";

should be
color = "blue";
if passing a reference is not needed & operator?
 

MrChips

Joined Oct 2, 2009
29,848
& operator is called the address operator.
For example, if we declare:

short num

&num passes the address of num

Arrays are slightly different.
char colour[ ] = "red";

colour is an array.

colour by itself passes the address of the array. There is no need for the & operator.
 

Thread Starter

Alex1700

Joined Jan 12, 2020
106
& operator is called the address operator.
For example, if we declare:

short num

&num passes the address of num

Arrays are slightly different.
char colour[ ] = "red";

colour is an array.

colour by itself passes the address of the array. There is no need for the & operator.
hi mr chip i still get error 1693577426876.png
 

WBahn

Joined Mar 31, 2012
29,510
Hi guys I am changing the content of the string (color) by calling a function in the main.c but I get some errors may I know how to solve this error?

View attachment 301787
You have several issues, one of which can really bite you.

The following statement in main():

colour[] = "red";

creates a character array with space for four elements and initializes those elements to the character codes (usually ASCII) for the three characters in the string literal, plus final NUL terminator character, '\0'.

The value of 'colour' is the address of the first character in this array. While 'colour' is, in many respects, a pointer to a string, it is not a variable of pointer type, it is a variable of array type and there are differences. In particular, you can't change where it points to.

In the statement:

printf("colour in main:%s", colour);

The function needs a pointer to a string to match up to the %s type specifier, the name of the address works for this because it is a convenient shorthand for '&colour[0]'. If you had wanted to print out the string starting with the second character, you could have used &colour[1], so this format has uses.

The function change_the_colour() requires a pointer to a char and, again, the name of the array serves this purpose and, again, you could have used '&colour[0]'.

In your attempted call:

change_the_colour(&colour);

You are trying to pass the address of where the variable colour is stored, but this really doesn't make sense if you think about it as it would be the address where the address of the first character of the array is stored. However, the C standard requires that the address of variable of array type evaluate to the address of the array and not the address of the variable, so this actually will work, but is not recommended.

Within the change_the_colour() function is where you really go off the rails, and for some fairly subtle reasons.

The statement:

*colour = "blue";

Says to set the variable pointed to by 'colour' (which is a single char value) to the address of the string literal "blue". It can't do this as they are incompatible types. The type of "blue" is a 'constant char *' since it is a string literal that is actually located within the body of the program code (typically right at the end of it), which '*colour' is a variable of type 'char'. This is what the error message is telling you.

Perhaps it would make it easier to think of the equivalent way of writing this statement, which is

colour[0] = "blue";

Now, you might think that you could fix this by changing the assignment to:

colour = "blue";

This will work (i.e., is legal), but it will not do what you think it will.

Within the function, 'colour' is a local variable that is initialized to the same address as where "red" is stored in main(). But this assignment changes the value of the local variable within the function to point to the address of the string literal "blue". It does NOT change the value of the 'colour' array in main().

This is a good thing, because if it did what you probably wanted it to do, you would have additional problems because the array in main() is only allocated for four elements and "blue" requires five, so anything you do to replace "red" with "blue" is going to stomp on memory that does not belong to that array, which can cause all kinds of problems.

The fix that was suggested to you, namely to use strcpy(), slams you right into this minefield as it blindly copies five characters into an array that is only large enough for four.

I'm assuming that the intent of the program is to set the initial colour in main() and then, after calling the function, have the colour in main() reflect the new color that was set by the function. The best way to do this is with dynamically allocated memory for colors. An alternative is to use an array that is allocated enough memory to handle any colour that might be used.

To see how changing colour within the function() doesn't change colour within main(), consider the following:

Code:
#include <stdio.h>

void change_colour(char *colour)
{
    printf("Original colour: %s\n", colour);
    colour = "blue";
    printf("Updated  colour: %s\n", colour);
}

int main(void)
{
    char colour[80] = "red";

    printf("Colour in main(): %s\n", colour);
   
    change_colour(colour);
   
    printf("Colour in main(): %s\n", colour);

    return 0;   
}

OUTPUT:

Colour in main(): red
Original colour: red
Updated  colour: blue
Colour in main(): red
Notice that the variable 'colour' in main() was explicitly allocated 80 characters. The first four are initialized by the "red" string and the rest are set to zero.

To have the changes reflected back into main(), do the following:

Code:
#include <stdio.h>
#include <string.h>

void change_colour(char *colour)
{
    printf("Original colour: %s\n", colour);
    strcpy(colour, "blue");
    printf("Updated  colour: %s\n", colour);
}

int main(void)
{
    char colour[80] = "red";

    printf("Colour in main(): %s\n", colour);
   
    change_colour(colour);
   
    printf("Colour in main(): %s\n", colour);

    return 0;   
}

OUTPUT:
Colour in main(): red
Original colour: red
Updated  colour: blue
Colour in main(): blue
This is dangerous code, because if we were to change the color to something containing more than 79 characters, we would overwrite the array bounds. This not only invites all kinds of undefined behavior, but it also opens the door for buffer overflow attacks if the user has the opportunity to enter the color being changed to. The result could be a complete compromise of the system.

To prevent that, you could do the necessary checks with something like:

Code:
#include <stdio.h>
#include <string.h>

#define MAXCOLORLEN (80)

void change_colour(char *colour)
{
    printf("Original colour: %s\n", colour);
    strncpy(colour, "blue", MAXCOLORLEN-1);
    colour[MAXCOLORLEN-1] = 0;
    printf("Updated  colour: %s\n", colour);
}

int main(void)
{
    char colour[MAXCOLORLEN] = "red";

    printf("Colour in main(): %s\n", colour);
   
    change_colour(colour);
   
    printf("Colour in main(): %s\n", colour);

    return 0;   
}
Note that while strncpy() prevents copying more than the specified number of characters to the destination string, which prevents out-of-bounds issues at this stage, it does NOT guarantee that the resulting destination string will be NUL-terminated. That is why the next line is needed.

Working with strings in C is tricky and it is very easy to make mistakes.
 
Last edited:

WBahn

Joined Mar 31, 2012
29,510
You have two errors, line 177 and line 196.

177 change_the_colour(&colour);

should to be
change_the_colour(colour);

The reason is colour by itself is already the address of the array.
This is not an error because the C language standard requires that references to the address of an value of array type be converted to a value of pointer type set to the first element of the array.
 

WBahn

Joined Mar 31, 2012
29,510
hi mr chip i still get error View attachment 301794
There are two problems.

First,

color = "blue";

is setting 'color' to the address of a string literal that is embedded in the code portion of the program. Whether this is allowed depends on the compiler and the target platform.

In the printf() call, you are passing it the first character of the array and not a pointer to the array. Get rid of the * in front of 'color' in the call.
 
Top