-3

I want to make a C program that will print morse code but I am having a trouble returning the value of Y to the main function, so that later I can use that value of Y to compare it with the another base string to print the answer. I also have a doubt, please clarify if you know the answer. If I make my length parameter global does the value of the input length from the main function get stored in that parameter? thanks in advance

int length;

int *callfunction(int x){
    int temp=x;
    int Y;
    static int r[1000];

    if(temp==0 || temp==1){
        Y=1;    // for a
    }
    else if(temp==2 || temp==3){
        Y=2;    //for b
    }
    else if(temp==4 || temp==5){
        Y=3;    //for c
    }
    else if(temp==6 || temp==7){
        Y=4;    // for d
    }
        for(int i =0;i<length;i++){
        r[i]=Y;
    }
    return r;
}

int main(){
    int i,j,k,*p[1000];
    char string[100];
    char string1[]={'A','a','B','b','C','c','D','d'};
    const char* string2[]={"•-","-•••", "-•-•","-••"};
    printf("Enter a string:");
    gets(string);
    printf("The entered string is:%s\n",string);
    length= strlen(string);
    printf("Length=%d\n",length);

    for(j=0;j<length;j++){
        for(i=0;i<8;i++)
            if(string1[i]==string[j]){
                p[i] = callfunction(i);
            }
        }
        for(i=0;i<length;i++){
            printf("%d",p[i]);  //I want display the Y number here
            printf("\n");
        }

    getchar();
    return 0;
}

The program executes without any errors but I don't get the actual value of Y when the execution is done, please help

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
Pseudo
  • 56
  • 1
  • 2
  • 12
  • 1
    You cannot really return arrays, you know that? – Sourav Ghosh Mar 13 '17 at 08:54
  • you can return decayed pointers on static arrays. That works (but I wouldn't do that :)) – Jean-François Fabre Mar 13 '17 at 08:56
  • 1
    The buffer in your function is static, there's only *one* buffer that is referenced by every `p[i]`, so take that into consideration. And you don't get anything meaningful printed because you try to print a pointer with `%d` here `printf("%d",p[i]);`. To print an array you have to iterate over it. – StoryTeller - Unslander Monica Mar 13 '17 at 08:56
  • You can return a pointer to the beginning of the array. Then you would have to know the offset from this pointer to the element at the array index you want to use. – Julian Heinovski Mar 13 '17 at 08:57
  • Don't use `gets`. Don't ***ever*** use `gets`. It is a dangerous function, prone to abuse and undefined behaviors. It has been deprecated since the C99 standard, and removed in the C11 standard. Use [`fgets`](http://en.cppreference.com/w/c/io/fgets) instead, or if you're on a POSIX platform (like Linux or macOS) use [`getline`](http://man7.org/linux/man-pages/man3/getline.3.html). – Some programmer dude Mar 13 '17 at 08:59

1 Answers1

0

p[i] is a pointer to an array, not a value.

The simplest solution is to redefine p to be an array of int instead of an array of pointers to int:

int p[1000];

Then redefine the function to take the array (as a pointer to the first element) and the length of the array as arguments:

void callfunction(int x, int *r, size_t length){ ... }

Then call the function passing the needed arguments:

callfunction(i, p, length);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621