if passing a reference is not needed & operator?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";
hi mr chip i still get error& 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.
You have several issues, one of which can really bite you.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
#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
#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
#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;
}
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.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.
Do NOT use strcpy() in this instance - that will result in an array bounds violation since "blue" is longer than "red", which is what the array was allocated for.Sorry.
copyString( ) is not a C library function.
https://www.geeksforgeeks.org/different-ways-to-copy-a-string-in-c-c/
It is strcpy( )
https://www.programiz.com/c-programming/library-function/string.h/strcpy
There are two problems.hi mr chip i still get error View attachment 301794
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
G | Programming from scratch | Programming & Languages | 26 | |
|
Exploring programming a XY6020L power supply via modbus | Power Electronics | 7 | |
![]() |
Embedded programming on Linux | Microcontrollers | 4 | |
P | Logic to reverse string in c programming | Programming & Languages | 22 | |
![]() |
string in c programming | Programming & Languages | 27 |
by Aaron Carman
by Jake Hertz
by Aaron Carman
by Duane Benson