2020-05-13

How can I correctly send actual parameters of integers to main to calculate the sum in C++?

The books tells me The declaration of the main looks like this:
int main(int argc, char* argv[])
or int main(int argc, char** argv)
It seems that int argc, char** argv are the only things I can send as actual parameters.
Now I don't want to deal with strings main.
I want to calculate the sum of integers sent to main and return the sum.
#include <iostream>
int main(int n, char** argv) {
        std::cout << n << std::endl;
        char** temp = argv;
        int sum = 0;
        int i = 0;
        while (*temp != NULL) {
                std::cout << i++ << ':' << *temp << std::endl;
                sum += *temp++;
        }

        return 0;
}
The above is my original thinking that fails to work.
I think argument must be array of argc of pointer to integer. So following is the updated code:
#include <iostream>

int main(int n, int* argv[]) {
        std::cout << n << std::endl; //print the number of the arguments passed
        int** temp = argv;
        int sum = 0;
        int i = 0;

        while (*temp != NULL) {
                std::cout << i++ << ':' << **temp << std::endl;
                if (*temp != argv[0])
                        sum += **temp;
                ++temp;
        }
        std::cout << "The sum of all integered entered is " << sum << std::endl;

        return 0;
}

After compiling the code with GCC, I enter ./a.out 1 2 3, and I get
4
0:778121006
1:3276849
2:3342386
3:1213399091
The sum of all integered entered is 1220018326

I know it is far from perfect but it is better than the first one.

I think temp(or argv) is degraded to pointer to a pointer to an integer. 

So **temp should be an integer. 

Why the print of **temp looks like a pointer?

How can I correctly send actual parameters of integers to main to calculate the sum?  

2020-05-12

Why does printing the name of an array fail to return a pointer but a string ?



#include <iostream>
#include <vector>

int main(void) {
        char a[] = "HELLO";
        std::cout << a << std::endl;
        return 0;
}

See above code
I learn from the book that

In char a[] = "HELLO"; a is automatically degraded to the pointer of the first element of array,

Here it should be 'H'

So a is a pointer

But when printing a ,

I get HELLO

Why?

C/C++中main函数接受外部参数的写法,以及如何传参数

原文:https://blog.csdn.net/selina8921/article/details/79141031
另外这个也可以看一下
https://www.cnblogs.com/larryking/p/5783564.html


C/C++语言中的main函数,经常带有参数argc,argv,如下:
int main(int argc, char* argv[])//argv[0]表示一个指针

int main(int argc, char **argv)// *argv同样表示一个指针

argc: 整数, 为传给main()的命令行参数个数。
char** argv / char *argv[]: 字符串数组。

argv[0] 为程序运行的全路径名

argv[1] 为在DOS命令行中执行程序名后的第一个字符串;
argv[2] 为执行程序名后的第二个字符串;
...
argv[argc]为NULL。


  注意事项:

        1. argv是一个指针数组,要明白它的存数格式。它的每个元素都是一个字符指针,数组的末尾是一个NULL指针。

        2. 也是由于argv是一个指针数组,它指向数组的第一个元素(是一个字符指针),所以*argv是一个指向字符指针的指针,所以可以将其声明为char **argv。即声明为:【 int main( int argc, char **argv ) 】

        3. 参数名称是不重要的,你可以声明为其它的名字,比如 【 int main( int a, char *b[] ) 】 都是可以的。

        4. 指针数组argv的第 1 个元素,即argv[0],是程序运行的全路径名,也就是你的可执行文件的绝对路径名。

        5. 如果你输入的参数中有空格,应该使用双引号括起来。



 实现一个程序:将所有参数打印出来。文件名为:Untitled2.exe 路径:D:\学习文档\C练习代码\Untitled2.exe

开始->运行->输入cmd->改变路径到你Untitled2.exe文件所在的地方 (输入D:改变到D盘, 然后输入:cd 文件路径)

输入:Untitled2.exe  a123 b3454 c443


#include<iostream>
using namespace std;
int main(int argc,char **argv) {
    char ** temp = argv;  /* 保留argv */
    cout<<argc<<endl;
    int i=0;
    while( *temp != NULL ){
       cout<<i++<<": "<<*temp<<endl;
       ++temp;
    }
    return 0;
}

2020-05-08

Why does a reference formal parameter fail to be changed in a C++ function?



#include <iostream>
int summ(const int(&ra)[4]) {
        int sum = 0;
        const int* const end = ra + 4;

        while (ra != end)
                sum += *ra++;//wrong

        std::cout << *ra << std::endl;//ok

        return sum;
}

int main(void) {
        int a[4] = { 1,2,4,5 };

        std::cout << summ(a) << std::endl;

        return 0;
}
  
 a is pointer to array of 4 of integers

 ra is reference to a 

 It means when ra changes, a changes also

 so std::cout << *ra << std::endl;//ok  works.

  But I haven't set ra to be const reference

 Why does an attempt to change value of ra in sum += *ra++; fail to work?

You may wonder why I asked this.

Because in one other situation, reference as a formal parameter can be altered.

Like the following in which rpa can be altered to 0.

#include <iostream>
void test(const int*& rpa) {
        rpa = 0;
        std::cout << rpa << std::endl;
}

int main(void) {
        int a = 1;
        const int* pa = &a;

        test(pa);

        return 0;
}

































2020-05-04

Why swapp(pa, pb); works but swapp(&a, &b) is wrong in C++? (swapp is a function)





#include <iostream>
void swapp(const int*& rpa, const int*& rpb) {
        const int* temp = rpa;
        rpa = rpb;
        rpb = temp;
}
int main(void) {
        const int a = 33;
        const int b = 99;
        const int* pa = &a;
        const int* pb = &b;

        swapp(pa, pb);
        swapp(&a, &b);//This is wrong

        return 0;
}


Now pa is the same as &a
Why swapp(pa, pb); works but swapp(&a, &b) is wrong?

How to Add a Directory to PATH in Linux

 https://linuxize.com/post/how-to-add-directory-to-path-in-linux/ When you type a command on the command line, you’re basically telling the ...