Home > OS >  How can I use a variable defined in main in another function? C
How can I use a variable defined in main in another function? C

Time:08-24

I use argv[] to get input from the user. Now I want to use argv[1] as intager in another function. So my variable is int k = atoi(argv[1]) defined in main, like you can see it in my code. But I don't know who I can pass this variable to the function called rotate. Up to this moment I only found answers using pointer which I don't know anything about since I am still new to C and programming. Thanks for your help!

Edit: You can ignore my German comments in the code xD

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
    
bool only_digits(string digits);
int rotate(string newletter);
    
int main(int argc, string argv[])
{
    int checkfordigits = only_digits(argv[1]);
    
    if (argc != 2)
    {
        printf("Usage ./caesar key\n");
        return 1;
    }
    else if (checkfordigits == 1)
    {
        printf("Usage ./caesar key\n");
        return 1;
    }
    
    int k = atoi(argv[1]);
    
    
    string input = get_string("plaintext: ");
    int encipher = rotate(input);
}
    
bool only_digits(string digits)
{
    for (int i = 0, j = strlen(digits); i < j; i  )
    {
        if (!isdigit(digits[i]))
        {
            return 1;
        }
        else if (isdigit(digits[i]))
        {
            //Kann das hier leer bleiben
        }
    }
    //weshalb brauche ich hier return?
    return 0;
}
    
    
//Soll aus dem input plaintext den geheimtext machen, um argv[1] im alphabet verschoben
int rotate(string newletter)
{
    int shiftletter = 1;
    for (int i = 0, j = strlen(newletter); i <= j; i  )
    {
        if (isalpha(newletter[i]) && isupper(newletter[i]))
        {
            //Erst aus ASCII alphabetical index machen damit buchstaben 0-25 sind statt 65 
            newletter[i] -= 65;
            newletter[i] = ((newletter[i]   shiftletter) % 26);
            newletter[i]  = 65;
        }
        else if (isalpha(newletter[i]) && islower(newletter[i]))
        {
            newletter[i] -= 97;
            newletter[i] = ((newletter[i]   shiftletter) % 26);
            newletter[i]  = 97;
        }
        else
        {
            newletter[i]  = 0;
        }
    }
    printf("ciphertext: %s\n", newletter);
    return 0;
}

CodePudding user response:

  1. If you only want to use its value you need to pass it as a parameter

example:

void foo(int x)
{
    x  = 1;
    printf("x = %d\n", x);
}

int main(void)
{
    int y = 5;
    foo(y);
    printf("y = %d\n", y);
}

https://godbolt.org/z/KrcPh473j

  1. If you want to modify it in the function you need to pass pointer to this variable. Example:
void foo(int *x)
{
    *x  = 1;
    printf("x = %d\n", *x);
}

int main(void)
{
    int y = 5;
    foo(&y);
    printf("y = %d\n", y);
}

https://godbolt.org/z/rEWfzjq98

You can pass as many parameters as you need to the function. Example:

void foo(int x, int y, int z)
{
    printf("sum = %d\n", x * y * z);
}

int main(void)
{
    int a = 1, b = 2, c = 3;
    foo(a,b,c);
}

https://godbolt.org/z/Kz9drfPq7

CodePudding user response:

You can pass k as a parameter to rotate just as how argc and argv are passed to main. You just need to change the function signature of rotate to int rotate(string newletter, int k), and then when you call rotate you need to do

int encipher = rotate(input, k);
  • Related