Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 38 39 [40] 41 42 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 96047 times)

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #585 on: December 15, 2010, 02:16:34 pm »

Uhm, I'm pretty sure nobody develops for this 50 year old machine I have stashed in the basement anymore. From what I've seen, most modern machines have at least three compilers available.
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #586 on: December 15, 2010, 02:17:41 pm »

Okay, simply put: How do I pass a charstar array between functions?

Simply put I wouldn't. Assuming you are trying to pass an array of strings I'd pass a const boost::array<std::string> reference.

However to answer your question.

function(const char*[10]) as 10 is the size of the array of pointers should work fine.

Depending on your compiler you might also be able to use function(const char**) for a multidimensional array of chars, but as you know it's 10 there is no need for this.

I need the function called to pass the charstar array to the "host", or the function that called it.

Or am I just taking the wrong approach?
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #587 on: December 15, 2010, 03:40:25 pm »

You'll want to return a pointer to a char array. Just give it the char* you need as the return parameter and be sure to adjust your function definition to (I think) char*. Make sure that the calling function is expecting a char* and not a char[] or your compiler's going to throw a fit.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #588 on: December 15, 2010, 05:36:55 pm »

a pointer to an array of char pointers:

*((char[])*)


voila.
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #589 on: December 15, 2010, 05:38:32 pm »

a pointer to an array of char pointers:

*((char[])*)


voila.

After the function ends, won't the charstararray disappear, causing the pointer pointing to it become a NULL pointer?

Gee, I'm confused.  :-\
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #590 on: December 16, 2010, 03:11:06 am »

Eh, I had to make the post twice, I forgot the part about,

*((char[])*) x;

x=(*((char[length])*)) malloc(sizeof(char)*length);

return x;

Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #591 on: December 16, 2010, 06:30:28 am »

Wait, isn't it *((char*)[]) ?

Blarg, I'll trust you to know this better than me. But can you explain what you just did, so I can use it?  :-\
« Last Edit: December 16, 2010, 06:36:12 am by lordnincompoop »
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #592 on: December 16, 2010, 06:36:07 am »

char**

sheesh.
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #593 on: December 16, 2010, 06:37:26 am »

char**

sheesh.

Would work if the variable was a simple charstar.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #594 on: December 16, 2010, 06:42:18 am »

char** is a pointer to a pointer to a char.

it's used for acessing something like:

{{a,b,c,d},{e,f,g,h,},[i,j,k,l}}
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #595 on: December 16, 2010, 10:27:09 pm »

char** and char*[] are both correct in forming an array of strings of variable length. char[]* would form an array of strings of constant length, but the memory would not go out of scope, as you fear. char[][] is the only thing that would go out of scope.

Even then, if you passed the return value to another char[][], the data would be copied. Only if you return from a char[][] to char** would the memory go out of scope.

But really you should at least be using std::vector<std::string> if you really don't want to use boost. Learn STL. It makes your life so much easier.
« Last Edit: December 16, 2010, 10:29:46 pm by Normandy »
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #596 on: December 17, 2010, 03:53:16 am »

But really you should at least be using std::vector<std::string> if you really don't want to use boost. Learn STL. It makes your life so much easier.

This or boost::array<std::string>

Only stick with the c strings if your forced to for some reason.

(also i'd recommend stlport as the version of stl you use, but most (all?) standard c libraries come with an implementation these days)
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #597 on: December 17, 2010, 04:07:01 am »

Can anybody with Linux experience help me?
As far as I can figure (and I've been figuring for a long time), when I run my program through the IDI, it creates a file in the directory the executable is in, but when I run it through the terminal, it creates a file in my home directory. Full path, as I first thought, doesn't suit me, because I'd like to distribute the program.
Actually, not only people with Linux experience. If I compile it on Windows, will it do this to me, too?
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #598 on: December 17, 2010, 05:16:35 am »

char**

sheesh.

Japa is right.

I guess I was getting all excited about the catch where "order of operations" applies to variable declarations.

Just forget what I said.

I haven't used C in awhile.
« Last Edit: December 17, 2010, 05:19:56 am by eerr »
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #599 on: December 17, 2010, 06:33:35 am »

Can anybody with Linux experience help me?
As far as I can figure (and I've been figuring for a long time), when I run my program through the IDI, it creates a file in the directory the executable is in, but when I run it through the terminal, it creates a file in my home directory. Full path, as I first thought, doesn't suit me, because I'd like to distribute the program.
Actually, not only people with Linux experience. If I compile it on Windows, will it do this to me, too?
If you want to create files in a local directory use "./file". In C it would look like this `FILE* file = fopen("./file", "wr");`. Should work in Windows too.

e, How are you running it? Do you `cd /path/to/executable; ./exe` or do you run `/path/to/executable/exe`? The files will be created where the user is (ie, the output of pwd), not where the executable is.
« Last Edit: December 17, 2010, 06:46:39 am by ILikePie »
Logged
Pages: 1 ... 38 39 [40] 41 42 ... 78