Skip to content

참조변수 argv[] 의 값에 접근하는 방법은 없나요..? #9

@inwoo123

Description

@inwoo123
  1. `#include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. int main (int argc, string argv[]) // how to implement argc2?
  6. {
  7.    if(argc!=2) // how to know more than 1 number was putted?
    
  8. {
    
  9.     printf("error : You should put 1 non-negative integer");
    
  10.     return 1;
    
  11. }
    
  12.     **int number = &argv[1];** : argv[1]에 입력된 값을 number라는 변수로 사용하고 싶습니다. 
    
  13.     printf("plaintext:\t");
    
  14.     string pltxt = get_string();
    
  15.     string cptxt = pltxt;
    
  16.     int key =0;
    
  17.     // get remainder of number devided by 26
    
  18.     for (int i=0, n=strlen(pltxt); i<n; i++) //iterate over the letter of pltxt
    
  19.     { //one by one letter
    
  20.         if (!isalpha(pltxt[i])) // see if i'th word is letter or not
    
  21.         {
    
  22.             continue;
    
  23.         }
    
  24.         if (islower(pltxt[i]))
    
  25.         {
    
  26.             key=pltxt[i]-'a';   // difference btween pltxt and a 
    
  27.             cptxt[i]=(key+number)%26+'a'; // add residual of (key+numner)/26 to a
    
  28.         }
    
  29.         if (isupper(pltxt[i]))
    
  30.         {
    
  31.             key=pltxt[i]-'A';   // diff between pltxt and A
    
  32.             cptxt[i]=(key+number)%26+'A';
    
  33.         }
    
  34.     }
    
  35.     printf("\n");
    
  36.     printf("ciphertext:\t");
    
  37.     printf("%s\n",cptxt);
    
  38. }`

이랬을 때 에러가

caeser.c:18:13: error: incompatible pointer to integer conversion initializing 'int' with an expression of
type 'string *' (aka 'char **') [-Werror,-Wint-conversion]
int number = &argv[1];
^ ~~~~~~~~
2 errors generated.
make: *** [caeser] Error 1
라고 뜨네요.

중간에 지워버린 코드가 있는데
if (&argv[1]<'0')
{
printf("error: You should put 1 non-negative integer");
return 1;
}
이 코드를 돌렸을 때도 argv[1]이 참조변수라서
caeser.c:13:21: error: ordered comparison between pointer and integer ('string *' (aka 'char **') and 'int')
[-Werror]
if (&argv[1]<'0')
이라고 뜨더라구요
그래서 argv[1]의 값을 int number라는 값변수에 저장하고 싶은데 &나 * 둘 중 하나를 사용하면 되는건가요?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions