C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 8th, 2003, 02:59 PM
Stargazer Stargazer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 8 Stargazer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question about sending multi-dimentional arrays in C++

Here's the gist. I have a 2-dimentional array in my .cpp file, and I want to send it to my .h file. I just want the information there so I can use it, and I can't figure out how to do it. Any help is appreciated.

Reply With Quote
  #2  
Old February 8th, 2003, 03:04 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189

you want to "send" it to your .h file? what is this supposed to be?!?!

you want to use an array in your .h file? dude, you didnīt understand the concept - it works the other way around! you "use" the arrays in your .cpp file that you have defined in your .h files...
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old February 8th, 2003, 03:12 PM
Stargazer Stargazer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 8 Stargazer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Perhaps I'll be more specific on why I need to do this.

I'm creating a .h file with a method in it called menu(). it recieves a string that tells it what to put at the top, and an 2-dimentional array for all of the items of the menu. Then it does a cool thing using the up and down arrows that I've already tested and it works.

I'm not making this for one individual program. I'm making this to put in my include library so that I can use it to no end. I need to be able to create the array in the .cpp file and have the .h file recieve it so that it can make the menu. Is that more clear?\

(All of this is in dos, in case that is where the mix up is)

Reply With Quote
  #4  
Old February 8th, 2003, 03:20 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
it is more clear, but again: you are mis-using the concept.

.h files are for describing abstract functions, variables, classes and macros (what did i forget?). you would only very very seldomly put actual function code there. the .cpp is for implementing the actual code.

for building a library, you would:
- define the interface in a .h file
- implement it in a .cpp file
- compile it to a .o file
- reuse the .o file, importing it into other projects using the .h file.

additionally, for being more professional than re-using the object file, you would build a library of it.

Reply With Quote
  #5  
Old February 8th, 2003, 03:28 PM
Stargazer Stargazer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 8 Stargazer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm sorry, but I'm lost.

I always put useful functions in .h files so that I don't have to rewrite it. (99 cases out of 100, inside a class). I have yet to hear someone else say not to put functions in a C++ .h file.

Examples:

pause()
cls() (clears the screen)
bubblesort()

All of which are in .h files and all of which are dead useful. menu() is one problem away from joining them. I don't understand why you wouldn't put methods in .h files. I'm just trying to understand what you're saying here.

Reply With Quote
  #6  
Old February 8th, 2003, 03:32 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
i tried to explain that in my last post.

c++ is not that strictly defined. you could put all your functions in a ".txt" file and compile this one. but itīs still no good style.

you could #define all your functions, but itīs still no good style

you could make a library inside a .h file, but itīs still no good style.

Do you get my point? just because it works, it is not automatically right...

I probably wonīt reply here anymore, iīll leave this to the other C++ veterans here. Maybe they have some better way to explain why not to do this. Sorry, I am not that deep into the theory

Reply With Quote
  #7  
Old February 8th, 2003, 03:36 PM
Stargazer Stargazer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 8 Stargazer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm thinking that would be a good idea as well, because this isn't getting anywhere. I just see this code:

#include <gary.h>

...

pause();


as a very effective way of doing things, and in this case, to pause the screen. I'll just come back later when others have replied, and hopefully I'll see an answer.

Reply With Quote
  #8  
Old February 8th, 2003, 03:42 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
perhaps i am wrong in my understanding of this problem, but it seems as tho all u want is a function that accepts an array as an argument??
make menu() be menu(char [], int arraySize)

simple as that i believe?

Reply With Quote
  #9  
Old February 8th, 2003, 05:02 PM
Stargazer Stargazer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 8 Stargazer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
For one dimentional arrays, yes, that works. For two dimentional arrays, it doesn't.

Reply With Quote
  #10  
Old February 8th, 2003, 05:18 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,383 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 49 m 30 sec
Reputation Power: 4080
See if the C-FAQ at http://www.eskimo.com/~scs/C-faq/q6.20.html answers your question.
[edit] Also http://www.eskimo.com/~scs/C-faq/s6.html and be sure to read sections 6.18 and 6.19 as well [/edit]

As to whether you should put code in .h files or not, M. Hirsch is entirely correct. By convention, code goes into .c files and function prototypes, structure definitions, #defines, external declarations etc should go into the .h file. There are also some very good reasons why this is so (it's not just a convention).

1. It is impossible to share a global variable among several source files in a project, if you put the declaration in the .h file
2. Code will compile slower, since the code in the .h file will be included by the preprocessor and compiled each time.
3. Impossible for you to build your own library of precompiled functions if you put code in the .h file.

See http://www.eskimo.com/~scs/C-faq/q10.6.html for more info.

Last edited by Scorpions4ever : February 8th, 2003 at 05:26 PM.

Reply With Quote
  #11  
Old February 8th, 2003, 05:23 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
oh sorry didnt see it was 2 dimensional.
but from the link above, it seems as though u would pass a 2 dimensional array nearly the same as 1 dimensional.
and like they said, the implementation should be separate from the definition. the only exception to this is when making Template classes as it wont compile correctly if the implementation/definition are separate, all definitions should be in the .h file. i believe i am right about this, at least that's how i was taught... do u agree with this hirsch and scorpion?

Last edited by infamous41md : February 8th, 2003 at 05:27 PM.

Reply With Quote
  #12  
Old February 8th, 2003, 10:02 PM
Stargazer Stargazer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 8 Stargazer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thankyou very much. It works. 5 lines to make a menu with infinite items on my computer.

Just for the heck of it, seeing as this is probably my last post on this board:

1. You don't want to use global variables. It gets too difficult to keep track of them in large programs.
2. I don't care about a .00001 and a .00002 milisecond difference. Code efficiency is what I care about.
3. By definition, everything in the include library is a .h file.

Thanks for the help. I really appreciate it.

Reply With Quote
  #13  
Old February 9th, 2003, 03:10 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
Big thanks, Scorpions4Ever. I hate to tell people not to do something, but not being able to explain why.

Reply With Quote
  #14  
Old February 11th, 2003, 08:38 PM
ahuimanu ahuimanu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Right Coast
Posts: 25 ahuimanu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 34 sec
Reputation Power: 0
library

M.Hirsch,

can you direct me to a place to learn more about this process:

for building a library, you would:
- define the interface in a .h file
- implement it in a .cpp file
- compile it to a .o file
- reuse the .o file, importing it into other projects using the .h file.

in *nix?

in windows?

Thanks!

J

Reply With Quote
  #15  
Old February 11th, 2003, 09:34 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,383 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 49 m 30 sec
Reputation Power: 4080
ahuimanu, you've described the process correctly. It applies to pretty much any OS/C compiler. Only the names of the tools used are different. See my explanation here http://forums.devshed.com/showthrea...&threadid=49606
about how libraries are built. Hope this helps!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Question about sending multi-dimentional arrays in C++

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap