Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - initializing char** from other functions
Hello. I have something that looks like this: #include <stdio.h> void foo(char **s) { s=new char*[4]; for (int i=0;i<4;i++) s[i]=new char[20]; } void main() { char **bar; foo(bar); for (int i=0;i<4;i++) printf("%s\n",bar[i]); } Why do I get an error about memory not being able to be read? (Sorry if that's not the exact message, it's a translation) I also tried with bcc5.5, msvc6 and djgpp2.95, and djgpp was the only one not to cause that error. What's wrong with the code? Jul 22 2003
You should write ***: void foo(char ***s) { *s=new char*[4]; for (int i=0;i<4;i++) (*s)[i]=new char[20]; } void main() { char **bar; foo(&bar); } What you did is initializing stack variable, which is not returned from function. So, after function foo(...) executed, the parameter bar remained unchanged. DJPP should not differ from other compilers, because your code certainly contains bug. Nic Tiger. "Carlos Santander B." <Carlos_member pathlink.com> wrote in message news:bfk11b$29dm$1 digitaldaemon.com...Hello. I have something that looks like this: #include <stdio.h> void foo(char **s) { s=new char*[4]; for (int i=0;i<4;i++) s[i]=new char[20]; } void main() { char **bar; foo(bar); for (int i=0;i<4;i++) printf("%s\n",bar[i]); } Why do I get an error about memory not being able to be read? (Sorry if Jul 22 2003
Thank you, very much. In article <bfk1uq$2abo$1 digitaldaemon.com>, Nic Tiger says...You should write ***: void foo(char ***s) { *s=new char*[4]; for (int i=0;i<4;i++) (*s)[i]=new char[20]; } void main() { char **bar; foo(&bar); } What you did is initializing stack variable, which is not returned from function. So, after function foo(...) executed, the parameter bar remained unchanged. DJPP should not differ from other compilers, because your code certainly contains bug. Nic Tiger. Jul 22 2003
Carlos Santander B. schrieb...I have something that looks like this: #include <stdio.h> void foo(char **s) { Jul 23 2003
|