简体   繁体   中英

how can we pass a return value as a input to a same function in c

  1. 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.
  2. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM