Explanation of strncpy_s() Usage for Class Parameters | Pointers & Const

  • Thread starter Thread starter yungman
  • Start date Start date
Click For Summary
The discussion revolves around the use of pointers and the `const` qualifier in C++ when dealing with strings and arrays. It clarifies that when passing a string to a class method, a pointer is necessary because arrays in C++ decay to pointers when passed to functions. This means that the function receives the address of the first element of the array, allowing for manipulation of the array's contents without copying the entire array. The need for `const` is emphasized as a safety measure, ensuring that the function does not modify the passed string, which is particularly relevant when using functions like `strncpy_s` that expect `const` parameters. The conversation also touches on the distinction between passing by value and passing by pointer, noting that while the address is passed, it is still considered passing by value. Overall, the discussion aims to demystify the use of pointers and `const` in C++ programming, especially for beginners.
yungman
Messages
5,741
Reaction score
294
I don't know why I need to use pointer in one case and not the other:
C++:
#include <iostream>
#include <cstring>
using namespace std;
const int NAME_SIZE = 51;
class Student
{protected:
    char name[NAME_SIZE]; 
public:
    Student(){ name[0] = '\0';}
//Why I need pointer here? Why I need const?
    Student(const char* n){set(n); }
    void set(const char* n)
    {    strncpy_s(name, NAME_SIZE, n, NAME_SIZE);
        name[NAME_SIZE - 1] = '\0';
         cout << " Name is: " << name << "\n\n";
    }
};
int main()
{   Student student("Jennifer Haynes");
//Why I don't need to use pointer below?
    char Ar1[NAME_SIZE] = "John", Ar2[NAME_SIZE] = "Paul";
    cout << Ar1 << ", " << Ar2 << "\n\n";
    strncpy_s(Ar1, NAME_SIZE, Ar2, NAME_SIZE);
    cout << Ar1 << ", " << Ar2 << "\n\n";
     return 0;
}
Why when using class, and pass parameter, I have to use pointers, but shown in line21 to 24, I don't need to use pointer?

Also, why I have to use const in line 11 and 12?

thanks
 
Technology news on Phys.org
yungman said:
Why when using class, and pass parameter, I have to use pointers,
The function definition needs to know what to expect the parameters to be.
but shown in line21 to 24, I don't need to use pointer?
Yes you do. The array name without an index is a pointer. Ar1 is a pointer to the beginning location of the array.
Also, why I have to use const in line 11 and 12?
If it is declared "const" someplace, the functions it is passed to are much safer if they know that they should not modify that parameter. Look at the parameter list of strcpy_s and you will see that it is declared "const". That is why you need to declare the parameters that lead to it as "const".
 
  • Like
Likes sysprog and yungman
FactChecker said:
The function definition needs to know what to expect the parameters to be.Yes you do. The array name without an index is a pointer. Ar1 is a pointer to the beginning location of the array.If it is declared "const" someplace, the functions it is passed to are much safer if they know that they should not modify that parameter. Look at the parameter list of strcpy_s and you will see that it is declared "const". That is why you need to declare the parameters that lead to it as "const".

I thought I know the answer on the pointer part, but you have a different take about the array is a pointer to start.

Please see whether my reasoning holds any water:
I was thinking the reason why it has to be passed as pointer because you cannot copy c-string like int that a = b. So you cannot simply pass by value when passing parameter to a function. That's the reason you have to pass by pointer and then in the function, do strncpy_s().

Thanks for explaining the const part. I am starting to develope a phobia when I see const. Don't even know how many times I got tripped by this.

Thanks
 
yungman said:
I was thinking the reason why it has to be passed as pointer because you cannot copy c-string like int that a = b.
That is a good start. The string is an array of character values, so a simple assignment will not do what a strcpy does.
So you cannot simply pass by value when passing parameter to a function.
In fact, you are passing by value, but the value is the address of the beginning of the array. Inside the called function you have to specifically treat it as an address. There are things that you can do in C++ that can not be done in C which are nice, but a little more advanced.
That's the reason you have to pass by pointer and then in the function, do strncpy_s().
Yes.
Thanks for explaining the const part. I am starting to develope a phobia when I see const. Don't even know how many times I got tripped by this.
It really is simple enough once you know what is going on. It is there to protect you against changing something that should not be changed.
 
  • Like
Likes sysprog and yungman
Hi factChecker:
I experimented with this. I read up cpt7 in the book, the book used a different method, that's the reason I was lost at the beginning. This is the program, I put comment in it:
C++:
//Passing c-string and int array to function
#include <iostream>
#include <cstring>
using namespace std;
const int sz = 25;
void WithPointer(const char *C, const int*A)// use pointer as array name is pointer
{    char Cr2[sz]; int Ar[2];
    strncpy_s(Cr2, sz, C, sz);
    for (int i = 0; i < 2; i++)
    {
        Ar[i] = A[i];
    }
    cout << " WithPointer: Cr2 = " << Cr2 << ", Ar[] = {"<< Ar[0]<< ", " << Ar[1]<< "}\n\n";
}
//Below is from Gaddis book
void WithBrace(const char C[], const int A[])
{    char Cr2[sz]; int Ar[2];
    strncpy_s(Cr2, sz, C, sz);
    for (int i = 0; i < 2; i++)
    {
        Ar[i] = A[i];
    }
    cout << " WithBrace: Cr2 = " << Cr2 << ", Ar[] = {"<< Ar[0]<< ", " << Ar[1]<< "}\n\n";
}
int main()
{    int Ar[] = { 1,2 };
    WithPointer("Alan", Ar);
    WithBrace("Alan", Ar);//used by Gaddis
    return 0;
}
I call the function "withBrace()" as the method used by the book.

One more easy program and I am done with the Gaddis Book! I am definitely chilling the champaign.

Thanks for the help.
 
Last edited:
  • Like
Likes FactChecker
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
89
Views
6K
  • · Replies 89 ·
3
Replies
89
Views
6K
Replies
5
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
6K