Click here to Skip to main content
15,888,351 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: array declaration Pin
Richard Andrew x6417-Jul-11 13:45
professionalRichard Andrew x6417-Jul-11 13:45 
GeneralRe: array declaration Pin
Richard MacCutchan17-Jul-11 22:59
mveRichard MacCutchan17-Jul-11 22:59 
AnswerRe: array declaration Pin
«_Superman_»17-Jul-11 15:45
professional«_Superman_»17-Jul-11 15:45 
GeneralRe: array declaration Pin
Richard Andrew x6417-Jul-11 16:14
professionalRichard Andrew x6417-Jul-11 16:14 
GeneralRe: array declaration Pin
«_Superman_»17-Jul-11 16:33
professional«_Superman_»17-Jul-11 16:33 
GeneralRe: array declaration Pin
Richard Andrew x6417-Jul-11 17:32
professionalRichard Andrew x6417-Jul-11 17:32 
GeneralRe: array declaration Pin
Richard MacCutchan17-Jul-11 23:08
mveRichard MacCutchan17-Jul-11 23:08 
GeneralRe: array declaration Pin
«_Superman_»18-Jul-11 9:10
professional«_Superman_»18-Jul-11 9:10 
Take a look at the following examples.

Say I have a function as below -
void fun(char (*p)[10])
{
    cout << *p << endl;    // Will output Hello
}
Now you will be able to call it as -
char name[10];
strcpy_c(name, "Hello");
fun(&name);
But you will get a compile error if you try something like -
char name[11];    // Wrong size
strcpy_c(name, "Hello");
fun(&name);
You could have a slightly different version using references -
void fun(char (&r)[10])
{
    cout << r << endl;    // Will output Hello
}

char name[10];
strcpy_c(name, "Hello");
fun(name);

char name2[11];    //Wrong size
strcpy_c(name2, "Hello");
fun(name2);    // Compile time error
So even though the code calling the function looks exactly like it does when using a function accepting a simple character pointer, it is a safe function.
This is exactly how the new safe string functions are implemented.
The advantage here is that you do not need to pass in a second size parameter to the function and the check for the size is done by the compiler at compile time.

But when you allocate memory using new, then it becomes something like -
char (*p)[10] = new char[5][10];
You can now pass this on to a function, but you would need another level of indirection -
void fun(char (**p)[10]){}
fun(&p);
Or
void fun(char (*&p)[10]){}
fun(p);

«_Superman 
I love work. It gives me something to do between weekends.


Microsoft MVP (Visual C++)

Polymorphism in C

GeneralRe: array declaration Pin
Richard MacCutchan18-Jul-11 9:19
mveRichard MacCutchan18-Jul-11 9:19 
QuestionControlling microphone gain using "mixer" API? Pin
Vaclav_17-Jul-11 7:40
Vaclav_17-Jul-11 7:40 
AnswerRe: Controlling microphone gain using "mixer" API? Pin
Mark Salsbery17-Jul-11 11:46
Mark Salsbery17-Jul-11 11:46 
GeneralRe: Controlling microphone gain using "mixer" API? Pin
Vaclav_17-Jul-11 12:15
Vaclav_17-Jul-11 12:15 
QuestionVS 2010 and XP Pin
RomTibi16-Jul-11 23:45
RomTibi16-Jul-11 23:45 
AnswerRe: VS 2010 and XP Pin
Richard MacCutchan17-Jul-11 1:04
mveRichard MacCutchan17-Jul-11 1:04 
GeneralRe: VS 2010 and XP Pin
RomTibi17-Jul-11 8:50
RomTibi17-Jul-11 8:50 
GeneralRe: VS 2010 and XP Pin
RomTibi17-Jul-11 17:45
RomTibi17-Jul-11 17:45 
GeneralRe: VS 2010 and XP Pin
Richard MacCutchan17-Jul-11 22:47
mveRichard MacCutchan17-Jul-11 22:47 
AnswerRe: VS 2010 and XP Pin
«_Superman_»17-Jul-11 3:12
professional«_Superman_»17-Jul-11 3:12 
QuestionRe: VS 2010 and XP Pin
bob1697217-Jul-11 6:04
bob1697217-Jul-11 6:04 
GeneralRe: VS 2010 and XP [modified] Pin
RomTibi17-Jul-11 8:51
RomTibi17-Jul-11 8:51 
QuestionButton Hover Effect Pin
john563216-Jul-11 5:43
john563216-Jul-11 5:43 
AnswerRe: Button Hover Effect Pin
«_Superman_»16-Jul-11 5:47
professional«_Superman_»16-Jul-11 5:47 
GeneralRe: Button Hover Effect Pin
john563216-Jul-11 5:52
john563216-Jul-11 5:52 
GeneralRe: Button Hover Effect Pin
«_Superman_»16-Jul-11 5:53
professional«_Superman_»16-Jul-11 5:53 
GeneralRe: Button Hover Effect Pin
john563216-Jul-11 5:55
john563216-Jul-11 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.