简体   繁体   中英

Can I modify a pointer value, from a function, if i receive the pointer address, with a pointer to pointer?

Here's basically the code:

void calculate(int, int, char *, char *, char **); //the prototype of the function
int main(){

  int option;
  int operation_int=0;
  char operation[14];
  char value1[20];
  char value2[20];
  char *result;

  result = (char *)malloc(20*sizeof(char));
  calculate(2, operation_int, value1, value2, &result);

  return 0;
}
void calculate(int option, int operation_integ, char *var1, char *var2, char **res){
  int i=0;
  int value1 = 0;
  int value2 = 0;
  int calc_result = 0;


  switch(operation_integ){

    case 1:

    switch(option){

      case 1:
       break;

      case 2:
      while( ( (var1[i] != '\0') || (var2[i] != '\0') ) ){
        if( (var1[i] != '\0') ){ calc_result= calc_result + ( var1[i] - '0' ) * pow(10,i);}
        if( (var2[i] != '\0') ){ calc_result= calc_result+ ( var2[i] - '0' ) * pow(10,i);}
        i++;
      }

      i=0;

      while(calc_result!= 0){ //HERE IS SOMETHING WRONG
        *(*(res+i)) = (char)calc_result % 10;
        calc_result = calc_result/10;
        i++;
      }
    }
  }
}

So of course, the result of this is "Segmentation Fault". I think i'm not using properly the pointer to pointer notation, or maybe it's not 'legal' to modify a pointer, from a pointer to pointer, from inside a function. So i'm quite lost with this.

. Can somebody give me some help here?

There's a possible seg fault somewhere else:

      while( ( (var1[i] != '\0') || (var2[i] != '\0') ) ){

If you'r varN arrays are something like this, for example:

var1 = {'2', '1', '\0', '3' .... (other non '\0' chars)};
var2 = {'3', '2', '5', '\0' .... (other non '\0' chars)};

Your while loop would continue and eventually may produce a segfault (if you're unlucky, your other variables could be set to 0, ending your while loop but messing your code in such a way that you could hardly notice until you get wrong return values) Especially since you don't seem to be initializing those arrays at all.

One problem: *(*(res+i)) .

malloc returns a pointer to a contiguous block of memory. You're sending calculate() the address of that pointer as &result , then adding i to that address. When you dereference this new address with the first (inner) * , you get the contents of some random memory for any i > 0 . If the contents of this random memory don't happen to be a valid memory address, you'll segfault when you try to dereference with the second (outermost) * .

I'm guessing you want to increment the pointer returned by malloc , in order to access a location in the block of memory it reserved.

You might try declaring your function like this:

void calculate(int, int, char *, char *, char *);

allocating memory and calling it like this:

result = malloc( 20 * sizeof(char) );
calculate(2, operation_int, value1, value2, result);

and setting the i th char in your result block like so:

*( res + i*sizeof(char) ) = (char)calc_result % 10

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