0

There are some problems thatconfuse me:

  1. The callee function needs return a structure, but there is not a structure statment in caller function. If i have to write the declaration in the calling function,it can not be called packaging function.

  2. If i return a structure pointer by callee function, but the structure is in the stack of the called function and will be destroyed after the end, which is not safe. Sometimes i get some warning or even wrong!

  3. I have a limited ideas but it not good. I put the structure into the heap by malloc and return the void*pointer. But this gave birth to a new problem :after each call to this function, in the caller, I cannot release the heap through the free() function,the complier can not identify variable name of structure pointer. I think it verey dangerous. I want when the callee function quit,it can be released by itself.

This is the first time I came to this website to ask questions and I just came into contact with c language,If there is something stupid please point it out.

I have to write the structure declaration outside. This program for judging prime number, and I want to package the founction "judging_number". I do not want to write the structure declaration when I want to call the founction "judging_number".

Please give me some help, I would be very grateful. Sorry, this is my fault. I compiled it with clang++, I saved it as *.cpp, but I wrote c code in it.

What I mean is, can I put the declaration in the called function to realize the function modularization, how can I not declare a structure before calling the function? Is there any way I can not write a declaration. Like use founction in stdio.h.It is as convenient as using the functions of the standard library. Only need to write a line of function call and pass parameters, the called function can return multiple results.

    #include <stdio.h>
    
    
    struct boundary{
            int L;
            int R;
        };boundary *range;
    
    int *get_number()
        {
            int *nPtr = (int *)malloc(sizeof(int));
            do
            {
                printf("Please enter a vaild number for judging prime number.Or enter the number 1 to quit.\r\n");
                scanf("%d", nPtr);
                if (*nPtr == 1)
                {
                    exit(1);
                }
            } while (*nPtr < 1);
    
            printf("The object is %d\r\n", *nPtr);
    
            return nPtr;
    }
    
    int judg_number(int N,boundary range){
        if (N%range.L==0&&N!=2){
            printf("The number %d is a composite number.\r\n", N);
        }
    
        else{
            printf("The number %d is a prime number.\r\n", N);
        }
    
        return 0;
        
    }
    
    boundary* get_range(int N){
    
    
        boundary *Ptr = (boundary *)malloc(sizeof(boundary));
    
        *Ptr = {2,N-1};
    
        printf("The range is between %d and %d .\r\n", Ptr->L, Ptr->R);
    
        return Ptr;
    }
    
    
    int main(int argc,char**argv,char**env){
        int*N;
        while(1){
            N=get_number();
            range=get_range(*N);
            judg_number(*N, *range);
            free(N);
            free(range);
        }
        getchar();
        getchar();
        return 0;
    }
CHAOSYD
  • 15
  • 1
  • 5
  • Are you compiling with a C++ compiler but tagging the Q as C? If you want C, use a C compiler. If you want C++, use C++ coding style. – Support Ukraine Jul 05 '21 at 07:18
  • 1
    You can return a struct from a function just as you can return an `int`, but you cannot return a _pointer_ to any local variable. – Jabberwocky Jul 05 '21 at 07:19
  • Now the Q is tagged as C. But it can't compile as C code... – Support Ukraine Jul 05 '21 at 07:24
  • Sorry, this is my fault. I compiled it with clang++, I saved it as *.cpp, but I wrote c code in it. – CHAOSYD Jul 05 '21 at 07:28
  • What do you mean by "package function"? – Simon Doppler Jul 05 '21 at 07:29
  • C and C++ are different. The syntax differs. Not all C code works as C++ and vice versa. And coding style should be completely different. If you want C, use a C tool chain. – Support Ukraine Jul 05 '21 at 07:29
  • What I want to express is how to modularize the function. When I want to implement a certain function, I only need to call it. I want to write it as printf and only need to pass parameters. – CHAOSYD Jul 05 '21 at 07:35
  • @CHAOSYD To me your description is unclear.... " I want to write it as printf and only need to pass parameters" hmm... are you talking about a function that can take a variable number of arguments of different types? – Support Ukraine Jul 05 '21 at 07:40
  • when i call the founction i only neeed write "juding_number()"and pass the parament.But in my code when i want to call it,i must write the struct declearation.I want to implement a function that judges prime numbers, and only call it in the calling function function, hiding the implementation details in the called function . – CHAOSYD Jul 05 '21 at 07:52
  • @CHAOSYD Now I think I understand your question. You want to hide the definition of the struct so that the caller doesn't know what the struct looks like. If so... then you are looking for an "opaque pointer". There are already several answers here at SO describing that - just do a search... Here is one that seems good for you: https://stackoverflow.com/questions/7553750/what-is-an-opaque-pointer-in-c – Support Ukraine Jul 05 '21 at 08:27
  • Another: https://stackoverflow.com/questions/60006166/c-struct-information-hiding-opaque-pointer – Support Ukraine Jul 05 '21 at 08:29

1 Answers1

0

You dont need dynamic memory allocation here. If you want to retun an int, retun an int. If you want to retun a stuct return a struct.

You probably want this:

#include <stdio.h>
#include <stdlib.h>

struct boundary {
  int L;
  int R;
}; 

struct boundary range;

int get_number()
{
  int n;
  do
  {
    printf("Please enter a vaild number for judging prime number.Or enter the number 1 to quit.\r\n");
    scanf("%d", &n);
    if (n == 1)
    {
      exit(1);
    }
  } while (n < 1);

  printf("The object is %d\r\n", n);

  return n;
}

int judg_number(int N, struct boundary range) {
  if (N % range.L == 0 && N != 2) {
    printf("The number %d is a composite number.\r\n", N);
  }

  else {
    printf("The number %d is a prime number.\r\n", N);
  }

  return 0;
}


struct boundary get_range(int N) {
  struct boundary b = { 2, N - 1 };
  printf("The range is between %d and %d .\r\n", b.L, b.R);
  return b;
}

int main(int argc, char** argv, char** env) {
  int N;
  while (1) {
    N = get_number();
    range = get_range(N);
    judg_number(N, range);
  }
  getchar();
  getchar();
  return 0;
}

BTW:

boundary get_range(int N) {... is invalid in C but valid in C++. In C it should be struct boundary get_range(int N) {...

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    Probably OP would benefit from a `typedef` but that's another story. – Support Ukraine Jul 05 '21 at 07:38
  • but when i call the founction “judg_number” i must write a stucture declaration befor i call it.Is threr not a idea to do not need to write the declaration,only call and pass the paraments? – CHAOSYD Jul 05 '21 at 07:41
  • @CHAOSYD I don't understan your question, `judg_number(N, range);` calls the function with parameters `N` and `range`, both of which have been declared beforehand... – Jabberwocky Jul 05 '21 at 07:48
  • Sorry,I am not a native English speaker.What I mean is, can I put the declaration in the called function to realize the function modularization, how can I not declare a structure before calling the function? Is there any way I can not write a declaration. – CHAOSYD Jul 05 '21 at 07:57
  • 1
    @CHAOSYD Your current `judg_number` takes an instance of `struct boundary` as parameter. When you do that, the definition of `struct boundary` **must** be known to the caller. End of story.... However, if you change the function so that it takes a **pointer to a** `struct boundary`, then you can hide the definition of `struct boundary` from the caller. Is that what you are looking for? – Support Ukraine Jul 05 '21 at 08:05
  • This is what I am looking for. I am a little struggling to read English, please forgive me. This is the first time I use English to ask questions in a professional field. I just used the translation tool and it took a long time. – CHAOSYD Jul 05 '21 at 08:14
  • @4386427 Can you send out the implementation of this hidden structure definition as code, I have learned a lot, thank you netizens for their help. – CHAOSYD Jul 05 '21 at 08:22
  • @CHAOSYD why on earth do you want to use a struct without declaring it beforehand? What's wrong with the my solution? And anyway in your original code you already declare `struct boundary` at the beginning of the program... – Jabberwocky Jul 05 '21 at 08:25
  • @Jabberwocky If you need to write a separate defintion before calling this function, you can send it to others and they will not be able to use it. And it’s also very troublesome, which is very unfriendly to me. – CHAOSYD Jul 05 '21 at 08:28
  • @CHAOSYD of course they will be able to used it. Maybe you should read about header files? It's absolutely unclear what problem you have with structs. BTW if your english is bad, you might want to try translete.google.com, it works pretty well. – Jabberwocky Jul 05 '21 at 08:39
  • @Jabberwocky OK,I will.you mean I can write the definition of the function "jude_number" and structure in the *.header file, and then include it? I'm a beginner, don't know much,I will try it now. – CHAOSYD Jul 05 '21 at 08:46
  • @Jabberwocky Thanks,I seem to have learned how to encapsulate functions. I am running very well now, thanks! ! ! – CHAOSYD Jul 05 '21 at 08:52
  • @CHAOSYD yes, exactly. Your learning material should include this. – Jabberwocky Jul 05 '21 at 08:58