This function aims to return the number of zeroes in a number, num. The function rCountZeros2() passes the result through
the pointer parameter result.
`
void rCountZeros2(int num, int *result)
{
if (num==0)
return;
else
{
if (num%10==0){
(*result)++;
}
rCountZeros2(num/10, result);
}
}
`
See when you are invoking rCountZeros2() , my guess is value in variable result is not zero.It may be some garbage value or some other value from previous computation.However with details you have provided it is difficult to provide exact answer.
Kindly try the following standalone program, I got correct answer using your code
void rCountZeros2(int num, int *result)
{
if (num==0)
return;
else
{
if (num%10==0){
(*result)++;
}
rCountZeros2(num/10, result);
}
}
int main()
{
int result = 0;
int num=12300000;
rCountZeros2(num, &result);
printf("number of zeros in %d = %d",num ,result);
}
Related
Im trying to do something easy, but i could find any solution.
I have an array, and I want to find an index, and update the pointer from a Function,
When Im trying to return the **Address of the index, and after it Print the Value at that index, I get nothing..
I hope you can Help me
int main()
{
int array[]={3,2,3,4};
int* pointer=NULL;
doIT((int *)array,4,pointer);
printf(" value of address %d",*pointer);
}
void doIT(int *p,int sizes,int** pointer){
for(int i =0;i<sizes;i++)
{
if(*p==4)
{
printf("TT %d",*p);
pointer=&p;
// printf("\n %d \n",pointer);
}
p++;
}
}
There are two errors: First, you have to call the function with
&pointer
and second, in the function body you have to set
*pointer=p;
In this function outdata to store a output then it pass as a input to this function then update the outputdata again pass then update like this manner.
again pass as input then update the outputdat.
char *SSWgetjson_createpacket(struct_json_sensor_buffer_ptr ret_data,struct_json_sensor_buffer_ptr outdata){
cJSON*array=cJSON_CreateArray();
cJSON*temp_root,*root=cJSON_CreateObject();
cJSON*rootout=cJSON_CreateObject();
if((cJSON_Parse(outdata->data)))
{
rootout=cJSON_Parse(outdata->data);
printf("outdata as a input");
}
else{
rootout=cJSON_Parse(ret_data->data);
printf("ret_data as a input\n");
}
//ret_data will works here.
temp_root=cJSON_GetArrayItem(rootout,0);
if(cJSON_GetObjectItem(temp_root,"NumPackets")){
cJSON_Delete(temp_root);
printf("numpacket key is present");
int num_packets=cJSON_GetObjectItem(root,"NumPackets")->valueint;//to check
printf("numpacket key contains value %d ",num_packets);
if(num_packets){
cJSON_SetIntValue(cJSON_GetObjectItem(root,"NumPackets"),++num_packets);
printf("numpacket key contains incremented value %d ",num_packets);
}
}else{
printf("key value not present\n");
cJSON_AddNumberToObject(root,"NumPackets",1);
cJSON_AddStringToObject(root,"Mac_id","12345");
}
ret_data->data=cJSON_Print(array);
int current_len=strlen(ret_data->data);
//printf("\n\noutdata printat packet function before memorycopy:%s\n",outdata->data);
//outdata->len=strlen(outdata->data);
printf("test %s \n",outdata->data);
if(outdata->len==0){
outdata->data=calloc(1,current_len);
outdata->len=current_len;
memcpy(outdata->data,ret_data->data,current_len);
printf("memory allocated outdata->len=%d current_len=%d \n",outdata->len,current_len);
}else{
printf("\n outdata_length:%d\n\n",outdata->len);
struct_json_sensor_buffer_ptr new=calloc(1,outdata->len+current_len);// to allocate memory then add a function.
new->len=ret_data->len +current_len;
printf("memeory reallocated %d \n",new->len);
memcpy(new->data,outdata->data,outdata->len);
memcpy(new->data+outdata->len,ret_data->data,current_len);
//free(outdata->data);
outdata->len=new->len;
outdata->data=new->data;
}
return outdata->data;
}
If I understood you correctly, you would use something like:
#include <stdio.h>
int function1(){
int some_value = 1;
return some_value;
}
void function2(int p){
printf("%d\n",p);
}
int main(){
function2(function1());
return 0;
}
/I am trying to return the 1st bit of boolean value of 10 using right shift in the cb function./
#include<stdbool.h>
bool cb(int N,int i){ //`called function`
return ((N>>i)&1)==1;
}
int main(void) { //`main function`
cb(10,1);
return 0;
}
//Status:successfully Executed,but no output.
main doesn't magically return the result of another function, you need to return the value also from main
int main(void)
{
return cb(10, 1);
}
or you can exit the program from your function with a value:
bool cb(int N,int i){ //`called function`
exit(((N>>i)&1)==1 ? EXIT_FAILURE : EXIT_SUCCESS);
}
and check the return in the console:
./yourprogram
echo $?
But notice that this is considered bad practice, we usualy return EXIT_FAILURE only when something went wrong, instead, you can print the result:
int main(void)
{
printf("%d\n", cb(10, 1));
return 0;
}
Finaly, you can use a debugger
Change your code to
Line 6 int res = cb(10, 1);
Line 7 return 0;
and start the debugger
gdb yourprogram
breakpoint 7 (after the line you want to inspect)
run
print res
So Here's your program:
#include<stdbool.h>
//`called function`
bool cb(int N,int i)
{
return ((N >> i) & 1) ==1;
}
//`main function`
int main(void)
{
cb(10,1);
return 0;
}
Your program is executing - which means that the main() function is returning successfully (a Value of 0). You also invoke cb(10,1); which calls your function declaration above (and returns a boolean: True/False). But you don't store the value of that function call, nor display the value with a printf() or cout statement.
You'll need to add more for your program to give you more noticable output.
I'm building a program that takes input as if it is a bare MAC address and turn it into a binary string. I'm doing this on a embedded system so there is no STD. I have been trying something similar to this question but after 2 days I haven't achieved anything, I'm really bad with these kind of things.
What I wanted is output to be equal to goal, take this into consideration:
#include <stdio.h>
int main() {
const char* goal = "\xaa\xbb\xcc\xdd\xee\xff";
printf("Goal: %s\n", goal);
char* input = "aabbccddeeff";
printf("Input: %s\n", input);
char* output = NULL;
// Magic code here
if (output == goal) {
printf("Did work! Yay!");
} else {
printf("Did not work, keep trying");
}
}
Thanks, this is for a personal project and I really want to finish it
First, your comparison should use strcmp else it'll be always wrong.
Then, I would read the string 2-char by 2-char and convert each "digit" to its value (0-15), then compose the result with shifting
#include <stdio.h>
#include <string.h>
// helper function to convert a char 0-9 or a-f to its decimal value (0-16)
// if something else is passed returns 0...
int a2v(char c)
{
if ((c>='0')&&(c<='9'))
{
return c-'0';
}
if ((c>='a')&&(c<='f'))
{
return c-'a'+10;
}
else return 0;
}
int main() {
const char* goal = "\xaa\xbb\xcc\xdd\xee\xff";
printf("Goal: %s\n", goal);
const char* input = "aabbccddeeff";
int i;
char output[strlen(input)/2 + 1];
char *ptr = output;
for (i=0;i<strlen(input);i+=2)
{
*ptr++ = (a2v(input[i])<<4) + a2v(input[i]);
}
*ptr = '\0';
printf("Goal: %s\n", output);
if (strcmp(output,goal)==0) {
printf("Did work! Yay!");
} else {
printf("Did not work, keep trying");
}
}
I'm Recently going through Divide and Conquer Algorithm.
I'm able to solve the problems if the return value supposes to be some single integer.
Ex:1. Binary Search, here I just need to return 1 if found, else -1.
Ex:2. Maximum Number in an array, just need to return a single number.
But when it comes to returning an array, like when we need the whole array as output(Ex: Sorting).
I feel it difficult.
Can anyone help with the best approach?
Below is my approach for Binary Search.
#include<stdio.h>
char* Divide(int arr[],int l,int r,int key)
{
int m=(l+r)/2;
if(l==r)
{
if(key==arr[m])
return "Found";
else
return "Not Found";
}
else
{
if(key==arr[m])
return "Found";
else if(key>arr[m])
Divide(arr,m+1,r,key);
else
Divide(arr,l,m,key);
}
}
int main()
{
int arr[]={1,2,3,4,5,6,7,8};
int n=sizeof(arr)/sizeof(arr[0]);
char* result=Divide(arr,0,n-1,10);
printf("%s\n",result);
return 0;
}
you would have to return the values in your recursive call try
#include<stdio.h>
char* Divide(int arr[],int l,int r,int key)
{
int m=(l+r)/2;
if(l==r)
{
if(key==arr[m])
return "Found";
else
return "Not Found";
}
else
{
if(key==arr[m])
return "Found";
else if(key>arr[m])
return Divide(arr,m+1,r,key); // just returning values here
else
return Divide(arr,l,m,key); // and here would make it work
}
}
int main()
{
int arr[]={1,2,3,4,5,6,7,8};
int n=sizeof(arr)/sizeof(arr[0]);
char* result=Divide(arr,0,n-1,10);
printf("%s\n",result);
return 0;
}
check the demo at online compiler