Jump to content
  • Advertisement
Sign in to follow this  
LordCube

C programming structure char arrays

This topic is 3382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm programming using SDL and C in DevC++. I am trying to create a struct array and within each struct have a char array. struct console_struct { int letteramount; int linewidth; int currentletter; int currentwidth; char lineletter[100]; }; struct console_struct console[CON_history]; However after I compile my code and start writing into the array I find its only like 8 chars long. The odd thing about it is when I change the specified size of the array (100) to something like 52. Then the array suddenly appears to be a different size but still not 52 chars long. Could any one please enlighten me into what I have done wrong and how to correct my problem?

Share this post


Link to post
Share on other sites
Advertisement
I have been debugging using printf etc. It returns garble once it passes 8 or so characters. The project is also visual. It's a type of console that I'm making where I can type into it. The lineletter arrays are each line of the console.

I have got the program to display what I type into the line. So when lineletter is defined as [100] and I type into the console it suddenly stops recieving input.

Input into the console is written like so
// Add character to the current console string
console[fastvar].lineletter[fastvar2+1] = '\0';
console[fastvar].lineletter[fastvar2] = c;
TTF_SizeText(font, console[fastvar].lineletter, &console[fastvar].linewidth, NULL);

Im positive that the problem lies in where the structure is defined.

Share this post


Link to post
Share on other sites
Just been messing around with it.
Rearranged it to this again


#define maxletter 100
struct console_struct
{
char lineletter[CON_maxletter];
int letteramount;
int linewidth;
int currentletter;
int currentwidth;
};
struct console_struct console[CON_history];


Compile, Run, *ERROR
Compile, Run, *ERROR
Compile, Run, works now?? wth?

Do you think this is the compilers fault?

Share this post


Link to post
Share on other sites
Quote:
Original post by LordCube
Compile, Run, *ERROR
Compile, Run, *ERROR
Compile, Run, works now?? wth?

Do you think this is the compilers fault?

No, and neither is the struct at fault. Some other part of your code is corrupting the stack/heap by writing to one of the arrays with an out of bound index, for example.

Share this post


Link to post
Share on other sites
Not working again :(


// PART OF GLOBALS.H

#define CON_history 25 // INIT // Amount of lines to go in console history
#define CON_width 800 // INIT // Width of the Application goes here
#define CON_height 600 // INIT // Height if the Application goes here
#define CON_lineh 15 // INIT // Height of each line - can be font h
#define CON_depth 10 // INIT // Amount of lines shown on console drop
#define CON_alpha 192 // INIT // Amount of alpha console background has
#define CON_dropspeed 5 // INIT // Speed that the console drops down
#define CON_maxletter 100 // INIT // Maximum amount of letters to go in a console line

extern int CON_pixeldepth;
extern int CON_cursorblink;
extern int consoletimer;
extern int shiftdown;
int shiftdown;
int CON_pixeldepth;
int CON_cursorblink;
int consoletimer;

/* Create the Console Array */
struct console_struct
{
char lineletter[CON_maxletter+1];
int letteramount;
int linewidth;
int currentletter;
int currentwidth;
};
struct console_struct console[CON_history];

// MAIN LOOP
while (!quit)
{
Update_game();

/* Check for events */
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
if (event.type==SDL_MOUSEBUTTONDOWN) {
// Mouse button down code here
}
break;
case SDL_MOUSEBUTTONUP:
if (event.type==SDL_MOUSEBUTTONUP) {
// Mouse button up code here
}
break;
case SDL_MOUSEMOTION:
if (event.type==SDL_MOUSEMOTION) {
// Mouse motion code goes here
}
break;
case SDL_KEYDOWN:
if (event.type==SDL_KEYDOWN) {
// Record current key pressed
current_key = event.key.keysym.unicode;
current_code = event.key.keysym.scancode;
// If shift is down
if ((current_code == 54)||(current_code == 42))
shiftdown = 1;
// Report key pressed
printf("%d\n", current_code);
// Input key into the console
Console_Input(current_key);
// If the key was esc then close app
if (current_code == 1)
quit = 1;
}
break;
case SDL_KEYUP:
if (event.type==SDL_KEYUP) {
current_key = 0;
shiftdown = 0;
}
break;
case SDL_QUIT:
if (event.type==SDL_QUIT) {
// Quit code here
quit = 1;
}
break;
default:
break;
if (current_key > 0) {
switch(current_key){
/* Escape Key */
case 1:
quit = 1;
break;
}
}
}
}
}
shutdown();
return 0;

// CONSOLE INPUT
/* Function to take and add pressed characters to the screen */
void Console_Input(key)
{
// If the enter key was pressed
if (current_code == 28) {
AcceptConsoleCommand();
ClearConsoleInputLine();
}
// If the key was backspace delete the last input
if (current_code == 14) {
if (console[CON_history].letteramount > 1) {
console[CON_history].lineletter[console[CON_history].letteramount-1] = '\0';
console[CON_history].letteramount--;
TTF_SizeText(font, console[CON_history].lineletter, &console[CON_history].linewidth, NULL);
consoletimer = 0;
return;
}
}
// Create fastvar variables
int fastvar = CON_history;
int fastvar2 = console[fastvar].letteramount;
// Insert Space if it was pressed
if (current_code == 57) {
InsertChar(' ');
}
// Insert special char if pressed
// '-'
if (current_code == 12) {
if (shiftdown == 1) {
InsertChar('_');
return;
}
InsertChar('-');
return;
}
// ','
if (current_code == 51) {
if (shiftdown == 1) {
InsertChar('<');
return;
}
InsertChar(',');
return;
}
// '.'
if (current_code == 52) {
if (shiftdown == 1) {
InsertChar('>');
return;
}
InsertChar('.');
return;
}
// Make sure key is a valid alpha numeral
if (isalnum(key)==0)
return;
// if shift is down
if (shiftdown == 1)
key = toupper(key);
// Convert to a character
char c = (char)key;
// If there is a space free for a new letter
if (console[CON_history].letteramount == CON_maxletter) {
return;
}
// Add character to the current console string
console[fastvar].lineletter[fastvar2+1] = '\0';
console[fastvar].lineletter[fastvar2] = c;
TTF_SizeText(font, console[fastvar].lineletter, &console[fastvar].linewidth, NULL);
// If the line is too long for this character
if (console[fastvar].linewidth >= CON_width) {
console[fastvar].lineletter[fastvar2] = '\0';
TTF_SizeText(font, console[fastvar].lineletter, &console[fastvar].linewidth, NULL);
} else {
// If the line is small enough to fit this character
console[CON_history].letteramount += 1;
}
}




[Edited by - Zahlman on September 10, 2009 8:19:09 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by LordCube
Not working again :(


// ...
struct console_struct console[CON_history];

// ...
if (console[CON_history].letteramount > 1) {
console[CON_history].lineletter[console[CON_history].letteramount-1] = '\0';
console[CON_history].letteramount--;
TTF_SizeText(font, console[CON_history].lineletter, &console[CON_history].linewidth, NULL);
// ...


All of these accesses to the 'console' array are out of bounds.

By the way, where do all these magic numbers for 'current_code' come from? And what kind of variable name is 'fastvar'?

And where is the code to move lines down in the array?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!