invalid conversion from char to const char - c

so i get this conversion problem char to const char. how can i get this code to work?
please, don't use pointers because im a newbie and i dont know anything about them... Thanks :)
#include<stdio.h>
#include<string.h>
char a[50];
int power(char a) {
int b;
b=0;
if(strlen(a)-b==0)
return 0;
else if(a[b]=='x'){
return power(a,b+1)+1;
}
else{
return power(a,b+1);
}
}
}
int main()
{
scanf("%s",&a);
printf("%d",power(a));
return 0;
}

power() should take an array (or a pointer, but you asked to keep those out, so...)
And b needs to be a parameter, not a variable that's always 0.
int power(char a[], int b) {
if(strlen(a)-b==0)
return 0;
else if(a[b]=='x'){
return power(a,b+1)+1;
}
else{
return power(a,b+1);
}
}
Finally, your scanf() call doesn't need a pointer to the address of the array:
int main()
{
scanf("%s",a);
printf("%d",power(a, 0));
return 0;
}

Related

How to fix the error "comparison between pointer & integer"?

I have been trying to compare a structure variable and a string variable. But I am getting this error.
#include<stdio.h>
#include<string.h>
int main()
{
struct details {
char number[20];
} det[10];
int inp, i=0;
char aad;
int b;
puts("Give The Number To Display The Pass Status");
scanf("%s", &aad);
for(b=0;b<i;b++)
{
if(det[i].number==aad)
{
printf("Hello");
}
}
return 0;
}
Pls try to fix my error
after you look in the comments this will be the answer
#include<stdio.h>
#include<string.h>
#define SIZE 20 // add size for aad
int main()
{
struct details {
char number[20];
} det[10];
int inp, i = 0;
char aad[SIZE]; // must be array (can be with pointer, use malloc)
int b;
puts("Give The Number To Display The Pass Status");
scanf("%s", &aad);
for (b = 0; b < i; b++)
{
if (strcmp(det[b].number, aad) == 0) // strcmp to compare strings
{
printf("Hello");
}
}
return 0;
}

Error with the array returning through function

I need to read a word from main function and convert the characters in UCASE if the first character is LCASE and vice versa using the user defined function.I tried ways for returning the array from function but still I am lacking some core ideas. Please debug this program and explain the way it works.
#include <stdio.h>
#include <string.h>
int* low (char str)
{
int i;
for (i=1; i<strlen(str);i++)
{
if(str[i]<91)
{
str[i]=str[i]+32;
}
else
{
}
}
return &str;
}
int* high (char str[50])
{
int i;
for (i=0; i<strlen(str);i++)
{
if(str[i]>91)
{
str[i]=str[i]-32;
}
else
{
}
}
return &str;
}
void main()
{
char str[50];
char* strl;
printf("Enter any string....\n");
scanf("%s",str);
if (str[0]<91)
{
*strl=low(str);
}
else
{
*strl=high(str);
}
printf("Converted string is %s.",*strl);
}
There is already a problem here:
So if you are saying this code is perfect and you want us to debug it and explain how (on earth) this works, then here you go.
In function int* low (char str), you have if(str[i]<91). Thats a problem right there. str is a char received as an argument, and hence str[i] is a straight compile-time error.
Another one to deal with is the return statement.
You have a statement:
return &str;
which would return the address of str, which by the way is a char, whereas function low is supposed to return a pointer to an int.
The same is applicable to high function as well.
Suggestion: Leave aside this bad code and get a beginner level C programming book first. Read it and the try some codes out of it.
A few inputs for improvement: (Which you may not comprehend)
change
void main()
to
int main(void)
Why? Refer this legendary post: What should main() return in C and C++?
Secondly, int both functions you are using strlen() in loop which will always return a fixed value. So, instead of
for (i=0; i<strlen(str);i++)
I'd suggest,
size_t strlength = strlen(str);
for (i=0; i < strlength; i++)
You can try the code and method as below:
#include <stdio.h>
#include <string.h>
char* caseConverter (char *str)
{
int i;
for (i=0; i<strlen(str);i++)
{
if(str[i]>=65 && str[i]<=90)
{
str[i]=str[i]+32; //To lower case
}
else if((str[i]>=97 && str[i]<=122))
{
str[i]=str[i]-32; //To upper case
}
else
printf("%c is not an alphabet \n",str[i]);
}
return str;
}
void main()
{
char inputStr[50]= "Stubborn";
char* opStr= caseConverter(inputStr);
printf("Converted string is %s",opStr);
}

How to send a pointer to a function?(This is different)

#include<stdio.h>
#include<conio.h>
int step_counter(char *array);
int main()
{
char *txt = "Try...";
printf("%d",step_counter(&txt));
getch();
}
int step_counter(char *array)
{
int step=0;
while(*array==NULL)
{
array++;
step++;
}
array-=step;
return step;
}
I need to send a pointer to a function without array. How can I solve this problem? I'm tired because of trying to solve this problem for months...
May be this is what you're trying to achieve.
#include<stdio.h>
int step_counter(char *array);
int main()
{
char *txt = "Try...";
printf("%d",step_counter(txt));
return 0;
}
int step_counter(char *array)
{
int step=0;
while(*array)
{
array++;
step++;
}
return step;
}
Edited
First, txt is a pointer to character array, so you don't have to send &txt to pass its address because txt itself is an address. And second, in the while loop you can either use while(*array) or while(*array != '\0') to check character array termination. And oh! as alk pointed out, array-=step; is redundant.

How do you pass a character array

I want to pass an array of characters i.e. a String in c
int main()
{
const char c[]="Joseph";
TestWord(&c,&c);
return 0;
}
int TestWord(char tiles[], char word[])
{
return tiles;
}
#include <stdio.h>
char *TestWord(char tiles[], char word[]);
int main()
{
char c[]="Joseph";
char r;
r = *TestWord(c,c);
return 0;
}
char *TestWord(char tiles[], char word[])
{
return tiles;
}
You pass through the arrays without the & as arrays don't need those, as they are already somewhat like pointers, just like how you would scanf an array without the & symbol.
Don't forget that if you are returning tiles that you should save that in a variable.
you could pass a string(character array) in C in many ways.
This code passes the string a to the function PRINT. Note that in this method the base address of the array is sent to the function.
#include<stdio.h>
void PRINT(char b[])
{
printf("%s",b);
}
int main()
{
char a[]="hello";
PRINT(a);
return 0;
}

Manipulating pointer values inside functions taking pointers as arguments

I have read a lot of questions of stackoverflow, but couldn't find any solutions on how to deal with this problem of allocating and manipulating pointers inside functions: Can anybody please tell me what's wrong with this code? (I want to allocate and assign values to *D through pointerpass and print the same through pointerprint)
#include<stdio.h>
#include<stdlib.h>
float *D;
void pointerpass(float **ptr1)
{
*ptr1=(float*)malloc(3*sizeof(float));
*(ptr1+0)=1.33;
*(ptr1+1)=2.33;
*(ptr1+2)=3.33;
}
void pointerprint(float **ptr2)
{
int j=0;
for (j=0;j<3;j++)
printf("\n%f\n",*(ptr2+j));
}
int main()
{
pointerpass(&D);
pointerprint(&D);
return 0;
}
Here we go
#include<stdio.h>
#include<stdlib.h>
float * pointerpass(){
float *ret = malloc(3*sizeof(float));
ret[0] = 1.33f;
ret[1] = 2.33f;
ret[2] = 3.33f;
return ret;
}
void pointerprint(float *array) {
int j=0;
for (j=0;j<3;j++) {
printf("\n%f\n",array[j]);
}
}
int main() {
float *x = pointerpass();
pointerprint(x);
free(x); // We do not like memory leaks
return 0;
}
void pointerpass(float **ptr1){
*ptr1=(float*)malloc(3*sizeof(float));
(*ptr1)[0]=1.33;
(*ptr1)[1]=2.33;
(*ptr1)[2]=3.33;
}
void pointerprint(float **ptr2){
int j=0;
for (j=0;j<3;j++)
printf("\n%f\n", (*ptr2)[j]);
}

Resources