Thread: Calling the value of a function inside another function C

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    4

    Calling the value of a function inside another function C

    Hi everyone!
    I'm trying to implement my own algorithm for a brute-force program in C but I can't figure out what I'm facing at a certain point. Here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define CHUNK 1024
    
    
    void combinations(char nums[],char candidate[],int current_len,int n3,int len) {
    
      if(current_len == len) {
          printf("%s\n", candidate);
          return;
      }
    
      else {
          int i;
          for(i=0; i < n3; i++) {
              candidate[current_len] = nums[i];
              combinations(nums,candidate,current_len+1,n3,len);
            }
      }
    
     }
    
    int main() {
    
        // ---- Reading the data from ASCII text ----
        int match[32];
        int length;
        char buf[CHUNK];
        FILE *file;
        size_t nread;
        file = fopen("data.txt", "r");
        if(file) {
          while((nread = fread(buf,1,sizeof buf, file)) > 0) {
            fwrite(buf,1,nread,stdout);
            match[32] = nread;
            length = nread-1;
            printf("\n%d\n", length);
          }
    
          fclose(file);
        }
    
        // -------------------------------------------
    
        char lc_letters[] =   {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v'};
        char up_letters[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V'};
        char nums[] = {'0','1','2','3','4','5','6','7','8','9'};
        char candidate[32];
        char output[32];
        int n = sizeof(lc_letters)/sizeof(lc_letters[0]);
        int n2 = sizeof(up_letters)/sizeof(up_letters[0]);
        int n3 = sizeof(nums)/sizeof(nums[0]);
        int count = length;
        char arr[10] = "";
        char new_str[21];
    
        combinations(nums,arr,0,n3,count);
    }
    Now let me explain this: what I'm trying to do is to read the text data from a file (OK here, it works) and then to do all the possible combinations of words or numbers. In this case I just created the combinations of the numbers in order to see if it works with one case and then go on like that with the others, but it's not like that. I can create my combinations with the void function called "combination" but here comes the problem:

    • Now I have both the data text and the combinations created, I can't figure out how to compare the data text with each combination created and print "YES" if it matches or "NO" if it doesn't.

    For example: this code reads the data text which is "12", so a text with length 2. Then the program creates all the combinations of numbers from 0 to 9 with length 2, so from 00 to 99.
    Now, can someone help me with this? I know it's not one of the best way to implement this algorithm but it's my own idea and I don't want to copy the one of another person.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    1. Remove all the dead code and variables from your code before asking other people to wade through it.
    2. Pick meaningful variable names - what's n3 ?
    3. The usual way to read a text file is to use fgets() rather than fread(). For one thing, fgets() will make sure there is a \0 marking the end of the string.

    > match[32] = nread;
    This is a buffer overflow.

    Have you considered passing buf to your function, so you have something to compare each combo with?
    combinations(nums,arr,0,n3,count,buf);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    4
    Quote Originally Posted by Salem View Post
    1. Remove all the dead code and variables from your code before asking other people to wade through it.
    2. Pick meaningful variable names - what's n3 ?
    3. The usual way to read a text file is to use fgets() rather than fread(). For one thing, fgets() will make sure there is a \0 marking the end of the string.

    > match[32] = nread;
    This is a buffer overflow.

    Have you considered passing buf to your function, so you have something to compare each combo with?
    combinations(nums,arr,0,n3,count,buf);
    Actually, to be honest I didn't consider the fact to pass my buf variable to the function. Now it works, thanks a lot for the help Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-24-2013, 01:05 PM
  2. Replies: 3
    Last Post: 03-14-2013, 03:25 PM
  3. Calling a function inside a function
    By kizyle502 in forum C Programming
    Replies: 1
    Last Post: 09-17-2009, 11:29 AM
  4. calling a function inside main()
    By mero24 in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2005, 01:22 AM
  5. calling a member function inside a vector
    By finnepower in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2004, 02:44 AM

Tags for this Thread