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

  • Thread starter yungman
  • Start date
In summary: Ar[i] << ", " << Cr2[i] << "\n"; Ar[i] = 0; } }#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
  • #1
yungman
5,718
241
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
  • #2
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
  • #3
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
 
  • #4
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
  • #5
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

1. What is the purpose of using strncpy_s() for class parameters?

The strncpy_s() function is used to copy a specified number of characters from one string to another. It is commonly used for class parameters to ensure that the copied string does not exceed the allocated memory space, preventing potential buffer overflow errors.

2. How does strncpy_s() handle null-terminated strings?

In addition to copying a specified number of characters, strncpy_s() also automatically appends a null character to the end of the copied string. This ensures that the string remains null-terminated and can be properly used in string manipulation functions.

3. Can strncpy_s() be used for pointers?

Yes, strncpy_s() can be used for pointers. It can be used to copy a specified number of characters from one pointer to another, similar to how it is used for class parameters. However, it is important to ensure that the destination pointer has enough allocated memory space to store the copied string.

4. How does strncpy_s() differ from strncpy()?

The main difference between strncpy_s() and strncpy() is that strncpy_s() is a more secure version of the function. It includes an additional parameter for the size of the destination buffer, which helps prevent buffer overflow errors. Strncpy() does not have this additional parameter and can be more prone to errors if not used carefully.

5. Can I use strncpy_s() for const class parameters?

Yes, strncpy_s() can be used for const class parameters. However, the destination pointer must also be declared as const, as the function will not be able to modify the contents of a const variable. It is important to ensure that the destination pointer has enough allocated memory space to store the copied string, as it cannot be resized for const parameters.

Similar threads

  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
5
Views
885
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
784
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top