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

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

The discussion centers on the usage of pointers and const qualifiers in C++ when dealing with class parameters and C-style strings. The function strncpy_s() is highlighted as a safe method for copying strings, necessitating the use of pointers due to the nature of C-style strings as arrays. The need for const in function parameters is explained as a means to ensure that the passed strings are not modified, aligning with the const declaration in strncpy_s(). Participants clarify that while array names act as pointers, direct assignment of C-style strings is not permissible.

PREREQUISITES
  • Understanding of C++ class structures and constructors
  • Familiarity with C-style strings and memory management
  • Knowledge of the strncpy_s() function and its parameters
  • Concept of const correctness in C++
NEXT STEPS
  • Study the differences between C-style strings and C++ string objects
  • Learn about const correctness and its implications in function parameters
  • Explore the usage of strncpy_s() and its alternatives in C++
  • Investigate how pointers and references work in C++ for function parameters
USEFUL FOR

C++ developers, software engineers working with legacy code, and students learning about memory management and string handling in C++. This discussion is particularly beneficial for those seeking to deepen their understanding of pointers and const usage in C++.

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 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];
    }
    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 FactChecker
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

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
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
6K