Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: C programming QUESTION  (Read 2475 times)

tilly

  • Bay Watcher
    • View Profile
C programming QUESTION
« on: February 01, 2014, 03:23:51 am »

Are there any experienced C language programmers that can answer this question for me?

Is it possible to declare an array and write a code that will increment the size(index total) of the array if needed?

example:

int age[10];
           ^ we want to increment this like a variable.



[Variable example]:



#include <iostream>
#include <conio.h>

int main(int argc, char** argv)
{
int x;
 x = 0;
       if (2 == 2)
       {
           x++;
       }         
printf("%d",x);
getch();

return 0;
}
« Last Edit: February 01, 2014, 03:31:58 am by tilly »
Logged

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: C programming QUESTION
« Reply #1 on: February 01, 2014, 03:30:53 am »

Arrays are static-size, and that's by design. But there are plenty of other ways you can do this, depending on what you are trying to do.

...What exactly are you trying to do? I don't even see any arrays in that code example.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

tilly

  • Bay Watcher
    • View Profile
Re: C programming QUESTION
« Reply #2 on: February 01, 2014, 03:34:29 am »

Arrays are static-size, and that's by design. But there are plenty of other ways you can do this, depending on what you are trying to do.

...What exactly are you trying to do? I don't even see any arrays in that code example.

Quite literally what I said. I want to increase the array index size through conditional code.

The example wasn't intended to portray an array or how I expect to do this.
Just the basic variable ++ increment for an example of what im talking about.

If by design this cant happen then I guess the best thing to do is to over extend it incase that space is needed for anything I may need later, correct?
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: C programming QUESTION
« Reply #3 on: February 01, 2014, 03:44:14 am »

I dont think you can do this with statically allocated arrays, you have to use pointers;

instead of

Code: [Select]
int stuff[10];

do

Code: [Select]
int* stuff = (int*)malloc(10 * sizeof(int));

The you can use realloc to change the size.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: C programming QUESTION
« Reply #4 on: February 01, 2014, 03:45:12 am »

Oh, you're iterating through the array. That makes more sense. In that case you'd want a while loop, something like this:

Code: [Select]
int x = 0;
while(x < 10)
{
    printf("%d",x);
    x++;
}

Unless you DID mean changing the size of the array, in which case what alexander said.
« Last Edit: February 01, 2014, 03:46:54 am by Gatleos »
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

tilly

  • Bay Watcher
    • View Profile
Re: C programming QUESTION
« Reply #5 on: February 01, 2014, 03:49:31 am »

I dont think you can do this with statically allocated arrays, you have to use pointers;

instead of

Code: [Select]
int stuff[10];

do

Code: [Select]
int* stuff = (int*)malloc(10 * sizeof(int));

The you can use realloc to change the size.

Very interesting!

Thank you for going above and beyond and actually sourcing your information. I appreciate the help in my learning. If I make any program worth it's weight in salt your names will surely be in the acknowledgements.
Logged

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: C programming QUESTION
« Reply #6 on: February 01, 2014, 03:54:48 am »

Keep in mind, if you use malloc() to allocate a block of memory you should later free it with free().
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

tilly

  • Bay Watcher
    • View Profile
Re: C programming QUESTION
« Reply #7 on: February 01, 2014, 04:01:15 am »

Keep in mind, if you use malloc() to allocate a block of memory you should later free it with free().

not to mention

#include <cstdlib>

so these functions become available. ;P

but, yes I will keep that in mind.
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: C programming QUESTION
« Reply #8 on: February 01, 2014, 04:06:28 am »

No problem.

If you are new to pointers, I suggest reading up about them as they are riddled with "gotchas" and all sorts of potential for !!Fun!!.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

tilly

  • Bay Watcher
    • View Profile
Re: C programming QUESTION
« Reply #9 on: February 01, 2014, 04:18:51 am »

No problem.

If you are new to pointers, I suggest reading up about them as they are riddled with "gotchas" and all sorts of potential for !!Fun!!.

Lol I'll be doing a lot of different reading on all of the C std library after hearing about pointers realloc's, malloc's ect.
I also just read how the index of an array can be a variable. Does this only apply to looping through arrays?

I'm going to write a quick program to see but, the questions here if you want to answer it.
Logged

tilly

  • Bay Watcher
    • View Profile
Re: C programming QUESTION
« Reply #10 on: February 01, 2014, 04:32:21 am »

Code: [Select]
#include <iostream>
#include <conio.h>

int main(int argc, char** argv)
{
int x;
int ages[x];

printf("Please type an age.\n");
scanf("%d",&ages[x]);
for (x = 0; x > -1; x++)
printf("%d\n",ages[x]);
scanf("%d",&ages[x]);




getch();

return 0;
}

Not going to lie, this did something very interesting. Very buggy and ends in a crash(edit: cycles through the entire int before exiting the loop) but, gives me something to try to understand. The numbers were always different untill it just randomly turned to 0's and then crashed.
« Last Edit: February 01, 2014, 04:42:28 am by tilly »
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: C programming QUESTION
« Reply #11 on: February 01, 2014, 05:05:09 am »

(iostream is a C++ library, so I am assuming you are compiling with a c++ compiler) In C++, you cannot allocate static arrays (arrays created with []'s ) and use a variable to specify the size.

So the code
Code: [Select]
int x = read_in_user_number();
int stuff[x];

is invalid as the compiler has no idea how big stuff is going to be at compile time, it depends on what the user inputs after it is compiled.

These are valid:

Code: [Select]
int stuff[5]; // Compiler knows "stuff" can ONLY be 5 elements long

Code: [Select]
const int x = 5;
int stuff[x]; // Compiler knows x will always and only be 5, and cannot be varied, and as such compiler knows "stuff" can only be 5 elements long


Here is some more hints on your program:

Code: [Select]

int main(int argc, char** argv)
{
int x; // x has not been set a value, and will most likely just be a seemingly-random, undefined number. this is referred to as "junk".

// This is not technically allowed in C++. You are using a variable (x) to try to specify the size of the array. The computer cannot know how long this will be when it is compiled.
int ages[x]; // x is still an unknown junk value, so you are trying to create an array whose length could be any number.

// x is still junk.
// arrays start at 0.
// To assign to the end of an array that is n long do something like:
//   myArray[n-1] = 5;     Which sets the last value of myArray to 5.
printf("Please type an age.\n");
scanf("%d",&ages[x]);

// In this for loop, you are now assigning x a value. Before it was a junk number, now it is zero.
// This loop will increase x by 1, starting at zero, until x is no longer greater than a negative number, than stop. (mathematically) x will always be greater than -1 as you are increasing x, and as such this is an infinite loop.
// This for loop needs curely brackets, or it will only loop the single next statement. In this case, the printf is in the for loop, the scanf is not.
for (x = 0; x > -1; x++)
printf("%d\n",ages[x]);  // Executed in for loop indefinetely until Bad Things happen.
scanf("%d",&ages[x]); // will not be executed (program cant get past for loop).

getch();


For for loops, this will print 0,1,2,3,4 on the screen:

Code: [Select]
int x;
for (x = 0; x < 5; x++)
{
    // All code in these brackets are looped. This is looped 5 times
    printf("%d", x);
}




Your program appears to read in ages and print them back out to the user. How many ages is the program supposed to read in?
« Last Edit: February 01, 2014, 07:01:14 am by alexandertnt »
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: C programming QUESTION
« Reply #12 on: February 01, 2014, 06:29:06 am »

Since you're including iostream, you're using C++. Since you're using C++, you have access to std::array and std::vector.

std::array is preferable to using a C array for fixed size arrays.
std::vector is the way to go if you want dynamic arrays that can resize themselves safely.
Logged