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

  • Thread starter Thread starter yungman
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the usage of pointers and const qualifiers in C++ when passing parameters to functions, particularly in the context of a class constructor and member function. Participants explore the differences between passing character arrays and other data types, as well as the implications of using const with string parameters.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants question why pointers are needed when passing parameters to class methods but not when using arrays directly in the main function.
  • It is noted that array names in C++ act as pointers to their first element, which may clarify why pointers are perceived as necessary.
  • Participants discuss the safety of using const qualifiers, suggesting that they prevent modification of parameters within functions, particularly in relation to the strcpy_s function.
  • One participant expresses confusion about the const qualifier and its implications, indicating a struggle with its usage in programming.
  • Another participant reflects on their understanding of passing strings as pointers, suggesting that copying C-strings cannot be done like simple variable assignments.
  • Examples from a textbook are shared, illustrating different methods of passing parameters to functions, including the use of pointers and array notation.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of pointers for passing character arrays to functions, but there is some contention regarding the understanding of why this is the case. The discussion remains unresolved regarding the best practices and implications of using const qualifiers.

Contextual Notes

Some participants mention confusion stemming from different methods presented in a textbook, indicating that there may be varying approaches to the same problem. The discussion reflects a range of understanding regarding pointers, arrays, and const usage in C++.

Who May Find This Useful

This discussion may be useful for students learning C++ who are grappling with pointers, arrays, and the const qualifier, particularly in the context of function parameters and class methods.

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
 
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   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

Similar threads

Replies
89
Views
7K
  • · Replies 89 ·
3
Replies
89
Views
6K
Replies
5
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 18 ·
Replies
18
Views
4K