Returning an array of pointers from a function

Karna

I am trying to print the integer value of each character in a given string using a function. When I pass a string to this function, it converts each character in the given string to an integer and then saves it to a result array. I have also created an array of pointers which will hold the address of each character's corresponding integer. But when I return this array of pointers, I am not able to print all integer values correctly.

#include <stdio.h>
int *c2i(char *str); 

int main(){

    char s[20];
    int *p, i;
    printf("Enter your string:\n");
    scanf("%s", s);
    p=c2i(s);
    for(i=0; i<4; i++){
        printf("\n%d\t%d\n", *p, p);
        p++;
    }

    return 0;
}

int *c2i(char *str){

    int i=0, result[20], *ptr[20];

    while(*(str+i)!='\0'){
        result[i]=*(str+i) & 0xff;
        ptr[i]=&result[i];
        i++;
    }
    printf("%d %d %d %d %d %d %d %d", *ptr[0], *ptr[1], *ptr[2], *ptr[3], ptr[0], ptr[1], ptr[2], ptr[3]);

    return *ptr;
}

I have included many printf statements just for debugging purposes. The output of the program when I try to run the code above is:

Enter your string:

abcd

97 98 99 100 6356588 6356592 6356596 6356600

97 6356588

1999382056 6356592

20 6356596

1 6356600

I am only getting the first value correctly when I print it in the main() function. When I increment the pointer and try to print the next values, I am getting garbage results. I can see that the address is the same but the values have been corrupted. What am I doing wrong here?

Srikanth Chekuri
#include <stdio.h>

int** c2i(char *str) {

    int i=0, *result, **ptr;
    result = (int *)malloc(20*sizeof(int)) ;
    ptr = (int **)malloc(10*sizeof(int *)) ;

    while(*(str+i)!='\0'){
        result[i]=*(str+i) & 0xff;
        ptr[i]=&result[i];
        i++;
    }
    printf("%d %d %d %d %p %p %p %p", *ptr[0], *ptr[1], *ptr[2], *ptr[3], ptr[0], ptr[1], ptr[2], ptr[3]);

    return ptr;
}
int main(){

    char s[20];
    int **p, i;
    printf("Enter your string:\n");
    scanf("%s", s);
    p=c2i(s);
    for(i=0; i<4; i++){
        printf("\n%d\t%p\n", **p, *p);
        p++;
    }

    return 0;
}

double-pointer might solve your issue, look at your modified code..this gives you the desired result.here i'm attaching the screen shot of execution..enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Array of pointers returning NULL from function

From Dev

Returning pointers from function as static variable

From Dev

Returning an Array from a Function in C

From Dev

Returning a mutable array from a function

From Dev

Returning an array from a function in VBA

From Dev

Returning an array from function in nodejs

From Dev

Returning vector array from a function

From Dev

Returning an array from function in C

From Dev

Javascript returning array from the function

From Dev

Function that returns a string from an array of pointers

From Dev

The pointers array returned from the function to the main is NULL

From Dev

Returning Array and assign to array from calling function

From Dev

Issue with returning an array of type int from function

From Dev

Returning an Associative Array from an awk function

From Dev

PHP function not returning a value from multidimensional array

From Dev

Returning array address from function did not work

From Dev

returning a bash array from a bash function

From Dev

returning array from function in c++

From Dev

returning array from function and assigning it to variable

From Dev

Wrong array values returning from a function

From Dev

returning array of string from function not working as expected

From Dev

Returning an array from function to main sub

From Javascript

Returning an array from a JSX function and destructuring it elsewhere

From Dev

Returning an array from a higher order function

From Dev

Type mismatch when returning an array from a function

From Dev

Returning an array from a function with arguments in c++

From Dev

Returning empty array from generic function

From Dev

Python: Returning array values from a function

From Dev

Explain syntax for returning array by reference from a function

Related Related

  1. 1

    Array of pointers returning NULL from function

  2. 2

    Returning pointers from function as static variable

  3. 3

    Returning an Array from a Function in C

  4. 4

    Returning a mutable array from a function

  5. 5

    Returning an array from a function in VBA

  6. 6

    Returning an array from function in nodejs

  7. 7

    Returning vector array from a function

  8. 8

    Returning an array from function in C

  9. 9

    Javascript returning array from the function

  10. 10

    Function that returns a string from an array of pointers

  11. 11

    The pointers array returned from the function to the main is NULL

  12. 12

    Returning Array and assign to array from calling function

  13. 13

    Issue with returning an array of type int from function

  14. 14

    Returning an Associative Array from an awk function

  15. 15

    PHP function not returning a value from multidimensional array

  16. 16

    Returning array address from function did not work

  17. 17

    returning a bash array from a bash function

  18. 18

    returning array from function in c++

  19. 19

    returning array from function and assigning it to variable

  20. 20

    Wrong array values returning from a function

  21. 21

    returning array of string from function not working as expected

  22. 22

    Returning an array from function to main sub

  23. 23

    Returning an array from a JSX function and destructuring it elsewhere

  24. 24

    Returning an array from a higher order function

  25. 25

    Type mismatch when returning an array from a function

  26. 26

    Returning an array from a function with arguments in c++

  27. 27

    Returning empty array from generic function

  28. 28

    Python: Returning array values from a function

  29. 29

    Explain syntax for returning array by reference from a function

HotTag

Archive