Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this program i did't get output i did't find out my mistake any one point-out my mistake and give me a hint only and any other method 's there give the hint to me only...




C#
#include<stdio.h>
#include<conio.h>

void fun(char *s, int l1);
void main()
{
//void rev(char, int);
char str;
int l;
clrscr();

printf("Enter the string: ");
scanf("%s",&str);
printf("%s",str);
//s=&str;
l = strlen(str);
fun(&str,l);
getch();
}

void fun(char *s,int l1)
{
 char *temp;

 if(l1 == -1)
 exit(0);

 else
  {
    temp = s+(l1-1);
    printf("%c",*temp);
    l1-=1;
    fun(s,l1);
  }

}

Posted

1 solution

The thing that immediately leaps out at one is the fact that you're using memory that isn't yours!

That is to say, your variable 'str' is a char - it holds just 1 byte of memory, yet in the scanf statement you're reading an unknown number of chars into just one byte of memory you own and (unknown number of chars - 1) into memory that follows.

You should:
  • Allocate some memory, either statically or dynamically
  • use scanf_s[^], which allows you to specify the max # of chars copied


Either use:
C++
const int bufSize = 1024;
char str[bufSize];  // or some other arbitrary num long enough to hold string
or
C++
const int bufSize = 1024;
char *str;
str = (char*)calloc(bufSize, 1);   // see above note on length - alloc & clear mem


And then use:
C++
scanf_s("%s", str, bufSize-1);


I haven't looked at the rest of the program. This may or may not fix your problem. In any case, it's still an error that you should fix.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900