My program is supposed to convert a string to lowercase but I keep gettingg this error "Argument type void is incomplete" when trying to convert a string to lowercase and I don't know why. Could anyone explain why this is happening, thanks
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
printf("%c", tolower((unsigned char) str[i]));
}
}
int main(void) {
upperToLower(str);
printf("%s\n", upperToLower(str));
return 0;
}
OUTPUT
I guess upperToLower is supposed to modify the string in place. Try the following code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
str[i] = tolower((unsigned char) str[i]);
}
}
int main(void) {
upperToLower(str);
printf("%s\n", str);
return 0;
}
You could also make upperToLower return the pointer that was passed to it (like strcpy does with its first parameter). That allows the the upperToLower call to be done as part of the printf call like this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
char *upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
str[i] = tolower((unsigned char) str[i]);
}
return str;
}
int main(void) {
printf("%s\n", upperToLower(str));
return 0;
}
The return type of the function upperToLower() is void, so it cannot be used as a value in expressions.
The function upperToLower() prints things inside that, so you won't need to externally print something about that except for the newline character.
Try this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
printf("%c", tolower((unsigned char) str[i]));
}
}
int main(void) {
upperToLower(str);
upperToLower(str); /* put this out of printf() */
printf("\n"); /* and print just newline character here */
return 0;
}
Yes. As specified in C specification the type void is an incomplete type.
The void type comprises an empty set of values; it is an incomplete
object type that
cannot be completed.
As result it is not possible to form any value of type void. Pointers of type void* can be used, but they cannot be dereferenced.
Your function returns void, you're literally passing nothing as a parameter.
Related
I am trying to run below program in an online C compiler. But I get segmentation error. Can you help me fix this
#include <stdio.h>
#include <string.h>
int main()
{
char string[15] = "Strlwr in C";
printf("%s",tolower(string));
return 0;
}
Following is the prototype of tolower
int tolower(int c);
You should pass an int or something like char which can safely convert to int. Passing char * (Type of string) like you do leads to UB.
To convert a string to lowercase, you need to convert each character separately. One way to do this is:
char string[15] = "Strlwr in C";
char lstr[15];
int i = 0;
do {
lstr[i] = tolower(string[i]);
} while(lstr[i] != '\0');
printf("%s", lstr);
You are using tolower incorrectly. This function returns int and gets int as a parameter (here is it's declaration: int tolower(int c);). What you want to do is call it on each char of your char array, and print each one:
char string[15] = "Strlwr in C";
for(int i = 0; i < strlen(string); i++)
printf("%c",tolower(string[i]));
Read cplusplus.com/reference/cctype/tolower It takes a single int as parameter, not char and not array.
You probably want to use a loop on "string", which processes each in turn.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
int i;
char string[15] = "Strlwr in C";
for (i=0; i< sizeof(string)/sizeof(char); i++)
{
string[i]=(char)(tolower((int)string[i]));
}
printf("%s\n",string);
return 0;
}
Output:
strlwr in c
I want to create a function to reverse a string in C. I found a couple pre-made on the internet but I wish to create mine. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* inverseCarac(char* chaine){
if(chaine==0||*chaine==0) return NULL;
int j, i;
int taille=strlen(chaine);
char* inverse=malloc(taille*sizeof(char));
for(j=taille-1, i=0; j>0; j--, i++){
*(inverse+i)=*(chaine-j);
}
return inverse;
}
int main(void){
char* test="bonjour";
char* inv=inverseCarac(test);
printf("%s", inv);
return 0;
}
I can't figure out why I get a segmentation fault.
There were several errors in your code, the most significant being the offset from chaine in the wrong direction. Also, lack of space for a string terminator, and j ending prematurely.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* inverseCarac(char* chaine){
if(chaine==0||*chaine==0) return NULL;
int j, i;
int taille=strlen(chaine);
char* inverse=malloc(taille+1); // add 1 for terminator
for(j=taille-1, i=0; j>=0; j--, i++){ // change j>0 to j >= 0
*(inverse+i)=*(chaine+j); // change -j to +j
}
inverse[taille] = '\0'; // write terminator
return inverse;
}
int main(void){
char* test="bonjour";
char* inv=inverseCarac(test);
printf("%s\n", inv);
return 0;
}
Program output
ruojnob
I am trying to use a function with a string as a parameter. I am running into a couple of error messages. First, it says that string[i] is not an array, pointer, or vector, despite the fact that string is a character array. Secondly, it says that I am doing a pointer to integer conversion. Here is my code:
#include <stdio.h>
#include <string.h>
void example (char string) {
int i;
for (i = 0; i < strlen(string); i++) {
printf (string[i]);
}
}
int main (void) {
example("I like pie");
return 0;
}
void example(char string) should be void example(char *string). You declared it to take a character, you want it to take a character pointer or array.
Also, you need to tell printf you are giving it a character: printf("%c", string[i]);.
Your API is wrong it should be
void example (char *string) { // string is a pointer
int i;
size_t n = strlen(string);
for (i = 0; i < n; i++) {
printf ("%c",string[i]); // print character using %c
}
}
Calculate the string length before the loop , calling strlen() in each iteration is not a good idea.
PS: what string points to is read-only you can't modify it
You should use void example(char *string) instead of void example (char string).
#include <stdio.h>
#include <string.h>
void example (char *string) {
int i;
for (i = 0; i < strlen(string); i++) {
printf ("%c",string[i]);
}
printf("\n");
}
int main (void) {
example("I like pie");
return 0;
}
Your function example just receives a character. To get a string you can use a pointer. Also you can use "%s" format specifier in printf instead of using the for loop and strlen() function.
#include <stdio.h>
#include <string.h>
void example (char *string) {
int i;
printf ("%s\n",string);
}
Hello I have a small problem during compilation.
So that problem in my program are:
brackets(str1); - Too few arguments to function call
void brackets(str,len) - Conflicting types for 'brackets'
Ny code -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void brackets(char str[], int len);
int main()
{
char str[99];
int len;
printf("enter Mathematical exercise: \n");
gets(str);
len = srtlen(str);
brackets(str1);
return(0);
}
void brackets(str,len)
{
char str1[99];
int i,j = 0;
for(i = 0; i < len; i++)
{
if (str[i] == '(')
{
i++;
while(str[i] != ')')
{
str1[j] = str[i];
i++;
j++;
}
}
}
printf("%s\n",str1);
}
I love to know what these errors arise and how can I arrange this program will work. Thanks
Change
void brackets(str,len)
to
void brackets(char str[], int len)
and your function call should be brackets(str, len);. And also change
len = srtlen(str); // Spelling mistake.
to
len = strlen(str);
your function brackets needs two parameters, so you need to give him two:
brackets(str1);
has to become
brackets(str1, len);
You're defining the brackets function as taking 2 arguments but passing only one. Pass the second one as well.
For your second error, if you don't provide a type for the function parameters the compiler will assume it's of type int. As the first parameter have already been declared to be of type char [] there is a mismatch between the declaration (prototype) and the definition of the function.
As for the first error, when you declare a function to take N parameters, you need to call it with N arguments. The exception being functions declare to take a variable number of arguments.
This is your issue: brackets(str1);
You called brackets with just one parameter, yet you define it as this:
void brackets(char str[], int len);
You need to pass an int as a second parameter.
multiple error seems in your code
Starting from main
you not declared str1 in main but used it in brackets(str1);.i think you mean it by str because you declared it char str[99];
so make change by brackets(str);
Also void brackets(str[], len) function not specify data type of function argument please change it to void brackets(char str[], int len)
Also in main len = srtlen(str); the srtlen is not valid but make it len = strlen(str);
And last you call brackets(str1); but brackets expect two argument so please make change in main by brackets(str,len);
After above change your code is now clean without error as follows
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void brackets(char str[], int len);
int main()
{
char str[99];
int len;
printf("enter Mathematical exercise: \n");
gets(str);
len = strlen(str);
brackets(str,len);
return(0);
}
void brackets(char str[],int len)
{
char str1[99];
int i,j = 0;
for(i = 0; i < len; i++)
{
if (str[i] == '(')
{
i++;
while(str[i] != ')')
{
str1[j] = str[i];
i++;
j++;
}
}
}
printf("%s\n",str1);
}
I am trying to write a small function to trim left spaces from a string, but I cannot get it right. In this version, I get the following error:
bus error: 10
Could anyone please explain to me what I am doing wrong? I am not looking so much for an alternative piece of code, but would like to understand the errors in my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void trim_string(char *);
int main(int argc, char *argv[]) {
char *temp = " I struggle with strings in C.\n";
trim_string(temp);
printf("%s", temp);
return 0;
}
void trim_string(char *string) {
char *string_trimmed = "";
int i=0, j=0;
while (isblank(string[i])) {
i++;
}
while (string[i] != '\0') {
string_trimmed[j] = string[i];
i++;
j++;
}
string_trimmed[j] = '\0';
strcpy(string, string_trimmed);
}
I have now found a workaround solution, shown below. But I am still not very clear about what I did wrong in the first place:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LENGTH 100
void trim_string(char [MAX_LENGTH]);
int main(int argc, char *argv[]) {
char temp[MAX_LENGTH] = " I struggle with strings in C.\n";
trim_string(temp);
printf("%s", temp);
return 0;
}
void trim_string(char string[MAX_LENGTH]) {
char string_trimmed[MAX_LENGTH];
int i=0, j=0;
while (isblank(string[i])) {
i++;
}
while (string[i] != '\0') {
string_trimmed[j] = string[i];
i++;
j++;
}
string_trimmed[j] = '\0';
printf("c\n");
strcpy(string, string_trimmed);
}
Both string and string_trimmed point to string literals, here in main:
char *temp = " I struggle with strings in C.\n";
^
|
This is a string literal
temp points to a string literal and the standard says you are not allowed to modify them.
In the function trim_string you are modifying a them which is undefined behavior of which a bus error is one possible result, although anything can happen.
string_trimmed either needs to be an array like this:
char string_trimmed[n] ;
where n is the size of your input using strlen(string) would probably make sense or dynamically allocated via malloc which you would need to free at the end of your function. The same things goes for your input from main, this would work as a substitute:
char temp[] = " I struggle with strings in C.\n";
For completeness sake, the draft C99 standard section 6.4.5 String literals paragraph 6 says (emphasis mine):
It is unspecified whether these arrays are distinct provided their elements have the
appropriate values. If the program attempts to modify such an array, the behavior is
undefined.