I have made a program to print out all ASCII character between two given character but then I write it in functions. The output of two these programs are different. I tried using pointer to pass variables by reference but the output is unlikely to be like the first program. What should I do for making it correctly ?
Here is the first program in C by using linear programming.
#include <stdio.h>>
int main()
{
char a,b,tmp;
int d;
scanf("%c%c",&a,&b);
if(a>b)
{
tmp=a;
a=b;
b=tmp;
}
d = b - a;
for (char c = a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
Here is the other program in functions.
#include <stdio.h>>
void ascii(char a,char b);
int main()
{
char a,b,tmp;
int d;
printf("Enter 2 character => ");
scanf("%c%c",&a,&b);
ascii(&a,&b);
}
void ascii(char a,char b)
{
int d;
if (a>b)
{
char tmp= a;
a=b;
b=tmp;
}
d=b-a;
for (char c=a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
you should not pass the addresses of your variables to your function the correct one is
#include <stdio.h>>
void ascii(char a,char b);
int main()
{
char a,b,tmp;
int d;
printf("Enter 2 character => ");
scanf("%c%c",&a,&b);
ascii(a,b);
}
void ascii(char a,char b)
{
int d;
if (a>b)
{
char tmp= a;
a=b;
b=tmp;
}
d=b-a;
for (char c=a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
Related
#include<stdio.h>
void add(int a,int b)
{
int c=a+b;
printf("\nSum=%d",c);
}
void hello(char *name)
{
printf("Hello %s",*name);
}
int main()
{
int a,b;
char name[20];
void (*ptr)(int,int)=&add;
void (*hello)(char*)=hello;
printf("Enter your Name:");
scanf("%s",&name);
hello(&name);
printf("Enter the two values\n");
scanf("%d%d",&a,&b);
ptr(a,b);
return 0;
}
i want to take input from user then pass it to function but i am unable to do so.
Here is what my complier shows as error: https://i.stack.imgur.com/DVYL6.png
You don't need to access the array address, it will be implicitly converted to char* when you pass it to the functions (both to scanf and hello).
I don't see the use of the functions pointers, so in order to simplify the code, I would rewrite it like this:
#include <stdio.h>
void add(int a, int b)
{
printf("Sum = %d\n", a + b);
}
void hello(char *name)
{
printf("Hello %s\n", name);
}
int main()
{
int a = 0, b = 0;
char name[20];
printf("Enter your Name:\n");
scanf("%s", name);
hello(name);
printf("Enter the two values\n");
scanf("%d%d", &a ,&b);
add(a, b);
return 0;
}
If you insist of using the pointers, this is how main should be written:
int main()
{
int a = 0, b = 0;
char name[20];
void (*add_ptr)(int, int) = &add;
void (*hello_ptr)(char *) = &hello;
printf("Enter your Name:\n");
scanf("%s", name);
hello_ptr(name);
printf("Enter the two values\n");
scanf("%d%d", &a, &b);
add_ptr(a, b);
return 0;
}
I need to find all suffix starting with a character X. For example, for int suffix (char str [], char c) when the word is ababcd and the letter b it should return:
babcd
bcd
and the number 2.
This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char c;
char str[128];
int counter=0;
printf ("Please enter charachter and a string \n");
scanf("%c %s",&c,str);
counter = my_suffix(str,c);
printf("The string has %d suffix \n",counter);
return 0;
}
int my_suffix(char str[],char c) {
int counter = 0;
for (int i=0; i < strlen(str); i++)
{
if (str[i] == c)
{ puts(str+i);
counter++;
}
}
return counter;
}
I couldn't find why it's not running,
Thanks!
Your code is fine you should just written following method above int main()
int my_suffix(char str[],char c){...}
so i have this C code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int b;
int c;
scanf("%d", &b);
scanf("%d", &a);
c = a + b;
printf(c);
return 0;
}
but, after i insert number for a and b, the program stop working. please help me
C noob here
In your code you have the following line is wrong:
printf(c);
as the printf() syntax would be like what i've written below
printf("%d",c);
so your now code would be:
#include <stdio.h>
int main()
{
int a;
int b;
int c;
scanf("%d", &b);
scanf("%d", &a);
c= a + b;
printf("%d",c); //this is the correct printf() syntax
return 0;
}
printf(c);
should be
printf("%d\n", c); /* `\n` at the end of the string flushes the `stdout` */
because printf expects a const char* as its first argument, not an int.
I have been asked to make a code that will rearrange 3 entered integers into ascending/descending order using pointers.
I need to use the function order() to return a pointer to either function ascending3() or descending(), depending on what value of 'e' is entered.
I keep getting an error on the line specified in the readInts() function and am not sure how to fix it.
ERRORS
lvalue required as unary ‘&’ operand" -- The error for `ptr = &(order(e))`
warning: assignment makes pointer from integer without a cast -- The error for `ptr=order(e)`
Pointer code
void readInts(){
int *a,*b,*c;
char e;
int (*ptr1)(int*,int*,int*);
int result;
printf("Enter Integer 1:");
scanf("%d", a);
printf("Enter Integer 2:");
scanf("%d", b);
printf("Enter Integer 3:");
scanf("%d", c);
printf("Enter either A or D:");
scanf(" %c", &e);
ptr1 = &(order(e)); /*ERROR HERE*/
result = (*ptr1)(a,b,c);
printf("%d %d %d", a, b, c);
}
Functions
int ascending3(int* x, int* y, int* z)
{
/*removed sorting code for the sake of shortening the question*/
*x=smallest;
*y=middle;
*z=largest;
}
int descending(int* x, int* y, int* z)
{
int swap;
ascending3(x,y,z);
swap=*x;
*x=*z;
*z=swap;
}
int (*order(char e))(int*x ,int*y,int*z)
{
if(e=='a')
{
return ascending3;
}
else if(e =='d')
{
return descending;
}
return;
}
A function cannot return a function. For this reason you cannot apply the address of operator (&) to the result of your function (to retrieve an address). But a function can return a pointer to a function.
The name of a function in C (prefixed or not by the & operator) is always set as the address of the function, that's a pointer to that function.
The correct code is:
int ascending3(int *x, int *y, int *z);
int descending(int *x, int *y, int *z);
typedef int (*fn)(int *x, int *y, int *z);
fn order(char e)
{
if (e == 'a')
{
return ascending3;
}
else if (e == 'd')
{
return descending;
}
return NULL;
}
void readInts(void)
{
int *a, *b, *c;
char e;
fn ptr1;
int result;
printf("Enter Integer 1:");
scanf("%d", a);
printf("Enter Integer 2:");
scanf("%d", b);
printf("Enter Integer 3:");
scanf("%d", c);
printf("Enter either A or D:");
scanf(" %c", &e);
ptr1 = order(e);
result = (*ptr1) (a, b, c);
printf("%p %p %p", a, b, c);
}
Where I used a typedef to declare the type of our function pointer (and also 2 prototypes for the ordering functions).
If you like to have the asterisk to better show the pointer nature of our type, you can define fn as function (not a pointer to function):
typedef int (fn)(int *x, int *y, int *z);
So you can use the asterisk notation:
typedef int fn(int *x, int *y, int *z);
fn *order(char e)
{
if (e == 'a')
{
return ascending3;
}
else if (e == 'd')
{
return descending;
}
return NULL;
}
void readInts(void)
{
int *a, *b, *c;
char e;
fn *ptr1;
int result;
printf("Enter Integer 1:");
scanf("%d", a);
printf("Enter Integer 2:");
scanf("%d", b);
printf("Enter Integer 3:");
scanf("%d", c);
printf("Enter either A or D:");
scanf(" %c", &e);
ptr1 = order(e);
result = (*ptr1) (a, b, c);
printf("%p %p %p", a, b, c);
}
This is an extended question from
How can i remove this Segmentation fault in C Program
here segmentation fault occur because of stack overflow due to recursion so manny times
so i have changed his code like this..
make a MACRO insted of that function so function call is removed
#include <stdio.h>
static inline void p(char *a, int b);
#define MAGIC(a,b) p(a,b)
void p(char *a, int b)
{
static long int i = 0;
if (i != 350000)
{
printf("\n%ld \t at Hi hello", i);
i++;
return MAGIC(a, b);
} else
{
return;
}
}
int main()
{
char *a = "HI";
int b = 10;
MAGIC(a, b);
printf("\nComplete");
return 0;
}
still i am getting segmentation fault ...still stack overflow.... why?
Change return MAGIC(a, b); to
goto START; and add a START label in the beginning of the function.
Edit:
Example using a while loop:
void p(char *a, int b)
{
static long int i = 0;
while (i != 350000)
{
printf("\n%ld \t at Hi hello", i);
i++;
}
}
Example using a for loop:
void p(char *a, int b)
{
long int i = 0; // static seems wrong here
for (;i != 350000; i++)
{
printf("\n%ld \t at Hi hello", i);
}
}
No, it will not work. A macro is just a text copy-paste, so the result is still the same.
So your code will be expanded as:
void p(char *a, int b)
{
static long int i = 0;
if (i != 350000)
{
printf("\n%ld \t at Hi hello", i);
i++;
return p(a, b);
} else
{
return;
}
}
int main()
{
char *a = "HI";
int b = 10;
p(a, b);
printf("\nComplete");
return 0;
}
which still has the recursion and will likely stackoverflow.
EDIT : One way to redesign the algorithm is as follows:
void p(char *a, int b)
{
long int i = 0;
while (i != 350000)
{
printf("\n%ld \t at Hi hello", i);
i++;
}
}