Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following code:
int (*predicate)(char) = 0;
Can anyone tell me what this code means? What is the meaning of the word predicate in C?
The sentence is a declaration and definition of a pointer to a function taking one argument (char) and returning int. The pointer is initialized to the null pointer value.
The word "predicate" is the programmer's choice for the variable name.
Reference: cdecl
One might use predicate like this:
/* UNTESTED */
int IsLower(char c) { return c >= 'a' && c <= 'z'; }
int main () {
int (*predicate)(char);
predicate = IsLower;
if ( (*predicate)('f') == 1 ) printf("'f' is lower case!\n");
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Below is a C program which wants to multiply an integer by 5 using bitwise operation. But when i run this program it gives unexpected output. I know there is something i messing up which i could not see. Any help regarding this guys and girls ?
#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int FiveTimes(int a)
{
int t;
t = a<<2 + a;
return t;
}
int main()
{
int a = 1, b = 2,c = 3;
PrintInt(FiveTimes(a));
PrintInt(FiveTimes(b));
PrintInt(FiveTimes(c));
return 0;
}
This is a question of "operator precedence": "<<" has a lower priority than "+" - so your code actually calculates a << (2 + a), while it should be (a << 2) + a. The latter is the fix.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have following C program and I am not understanding some point of this program
#include <stdio.h>
int main()
{
char ara[100];
while(NULL != gets(ara))
{
printf("%s\n", ara);
}
return 0;
}
If I input some string like Hello World, this code return me the output same as input. But, what is NULL and gets?? Are they from C library? Why their colour not changed when I compile them?
Please read description of function gets()!
This function reads string from stdin. It returns NULL if found end of line or end of file before any characters.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Task says:
Given a string, compute recursively a new string where all the 'x' chars have been removed.
My code:
#include<stdio.h>
#include<string.h>
char c[50];
int xx(char a[],int b,int d){
if(a[b]=='\0')
return a;
else if(a[b]=='x'){
c[d]=a[b+1];
return xx(a,b+2,d+1);}
else {
c[d]=a[b];
return xx(a,b+1,d+1);
}
}
int main()
{
char a[50];
scanf("%s",a);
xx(a,0,0);
printf("%s",c);
return 0;
}
As long as I don't type x next to the other x it works. Like if i type xaxb, the result will be ab.
But if I type xxaxxb, the result will be xaxb...
Your code skips over a potentially important character - a '\0' or an 'x' in these three lines:
else if(a[b]=='x'){
c[d]=a[b+1];
return xx(a,b+2,d+1);
}
This code goes ahead and copies the a[b+1] without checking that character at all.
You shouldn't copy anything there - just advance b by 1, and keep d as is:
else if(a[b]=='x'){
return xx(a,b+1,d);
}
This way the next level of invocation would check a[b+1] for you, stopping or removing it as needed.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to check for certain condition. If that is true, certain operation will be performed and then need to break out of function. If condition not satisfied then proceeding normal operations in the function will continue.
The return keyword is used to exit from a function.
If the function has a non-void return type, an expression of the appropriate type must be used with return to provide the return value:
/* Returns 2 * abs(a), where abs(a) denotes the absolute value of a. */
int twice_positive(int a)
{
if(a < 0)
return -2 * a;
return 2 * a;
}
Some people consider "early return" from long(er) functions to be evil stylistically and to be avoided; I don't agree but I thought I should mention it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let us say that the English alphabets A to Z has value start from 1. A = 1, B = 2, C = 3 and so on. Write a program which calls a function which accepts the name of a person which is constant character array and returns an integer value with sum of the Alphapets. What is the benefit of passing the name of the person as const char array?
Suppose somebody else provides me a function that takes non-const char * and does the job. What the function is actually implemented is like this:
int get_int_sum(char *name)
{
int sum;
//codes to calculate sum of alphas
name[0] += 1;
//continue
return sum;
}
When I call the function using
char my_name[] = "Yu Hao";
int reuslt = get_int_sum(my_name);
Even if I got the result I want, my_name is changed to "Zu Hao" without my notice. However, if a function has a prototype of
int get_int_sum(const char*name)
I am sure that the string I passed will not be modified.
One advantage is that elements in a array are protected from changing its value.
For example, here is simple code.
int Your_function(const char * a)
{
a[3] = 'A'; // this statement causes compile error.
// do something
return 0;
}