Click here to Skip to main content
15,843,498 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: array declaration Pin
Richard Andrew x6417-Jul-11 14:45
professionalRichard Andrew x6417-Jul-11 14:45 
GeneralRe: array declaration Pin
Richard MacCutchan17-Jul-11 23:59
mveRichard MacCutchan17-Jul-11 23:59 
AnswerRe: array declaration Pin
«_Superman_»17-Jul-11 16:45
professional«_Superman_»17-Jul-11 16:45 
GeneralRe: array declaration Pin
Richard Andrew x6417-Jul-11 17:14
professionalRichard Andrew x6417-Jul-11 17:14 
GeneralRe: array declaration Pin
«_Superman_»17-Jul-11 17:33
professional«_Superman_»17-Jul-11 17:33 
GeneralRe: array declaration Pin
Richard Andrew x6417-Jul-11 18:32
professionalRichard Andrew x6417-Jul-11 18:32 
GeneralRe: array declaration Pin
Richard MacCutchan18-Jul-11 0:08
mveRichard MacCutchan18-Jul-11 0:08 
GeneralRe: array declaration Pin
«_Superman_»18-Jul-11 10:10
professional«_Superman_»18-Jul-11 10: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 10:19
mveRichard MacCutchan18-Jul-11 10:19 
QuestionControlling microphone gain using "mixer" API? Pin
Vaclav_17-Jul-11 8:40
Vaclav_17-Jul-11 8:40 
AnswerRe: Controlling microphone gain using "mixer" API? Pin
Mark Salsbery17-Jul-11 12:46
Mark Salsbery17-Jul-11 12:46 
GeneralRe: Controlling microphone gain using "mixer" API? Pin
Vaclav_17-Jul-11 13:15
Vaclav_17-Jul-11 13:15 
QuestionVS 2010 and XP Pin
RomTibi17-Jul-11 0:45
RomTibi17-Jul-11 0:45 
AnswerRe: VS 2010 and XP Pin
Richard MacCutchan17-Jul-11 2:04
mveRichard MacCutchan17-Jul-11 2:04 
GeneralRe: VS 2010 and XP Pin
RomTibi17-Jul-11 9:50
RomTibi17-Jul-11 9:50 
GeneralRe: VS 2010 and XP Pin
RomTibi17-Jul-11 18:45
RomTibi17-Jul-11 18:45 
GeneralRe: VS 2010 and XP Pin
Richard MacCutchan17-Jul-11 23:47
mveRichard MacCutchan17-Jul-11 23:47 
AnswerRe: VS 2010 and XP Pin
«_Superman_»17-Jul-11 4:12
professional«_Superman_»17-Jul-11 4:12 
QuestionRe: VS 2010 and XP Pin
bob1697217-Jul-11 7:04
bob1697217-Jul-11 7:04 
GeneralRe: VS 2010 and XP [modified] Pin
RomTibi17-Jul-11 9:51
RomTibi17-Jul-11 9:51 
QuestionButton Hover Effect Pin
john563216-Jul-11 6:43
john563216-Jul-11 6:43 
AnswerRe: Button Hover Effect Pin
«_Superman_»16-Jul-11 6:47
professional«_Superman_»16-Jul-11 6:47 
GeneralRe: Button Hover Effect Pin
john563216-Jul-11 6:52
john563216-Jul-11 6:52 
GeneralRe: Button Hover Effect Pin
«_Superman_»16-Jul-11 6:53
professional«_Superman_»16-Jul-11 6:53 
GeneralRe: Button Hover Effect Pin
john563216-Jul-11 6:55
john563216-Jul-11 6: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.