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

  • Thread starter Thread starter yungman
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
yungman
Messages
5,741
Reaction score
291
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';
         count << " 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";
    count << Ar1 << ", " << Ar2 << "\n\n";
    strncpy_s(Ar1, NAME_SIZE, Ar2, NAME_SIZE);
    count << 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
 
Physics 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   Reactions: 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   Reactions: 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];
    }
    count << " 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];
    }
    count << " 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   Reactions: FactChecker