|
-
May 25th, 2001, 08:26 PM
#1
Thread Starter
Member
Arrays of chars
I am having great difficulty getting this code to work.
Code:
#include <string.h>
#include <stdio.h>
void main(){
const int numsqu=30;
char square[numsqu][numsqu];
int i=0, x=0;
for (i = 0;i<numsqu;i++){
for (x = 0;x<numsqu;x++){
square[i][x] = ' ';
}
}
char row[numsqu][numsqu];
char tempp[numsqu];
for (i = 0;i<numsqu;i++)
sprintf(row[i],"\0");
for (i = 0;i<numsqu;i++){
for (x = 0;x<numsqu;x++){
sprintf(tempp,"%s",row[i]);
sprintf(row[i],"%s%s \0",tempp,square[i][x]); //Problem Code!
}
}
}
Am I missing something simple? It compiles fine, but I cannot determine where the error is coming from. I'm using VC++ 6.
Thanks for any help,
chilibean
Last edited by chilibean; May 26th, 2001 at 09:04 AM.
-
May 25th, 2001, 09:39 PM
#2
Frenzied Member
I don't think you can set an array to a length that's a variable, dennis wrenn showed me a way around it, don't really remember. Try making it a pointer (put a * in front of it)
-
May 25th, 2001, 10:02 PM
#3
Your array declaration is fine, because you are using a const value. Other wise, the compiler would have yelled. The problem is in the second call to the sprintf function. Im not sure exactly what it is, but that is your starting point.
Z.
-
May 26th, 2001, 08:57 AM
#4
Thread Starter
Member
Should I use pointers?
Pointers would probably work fine, but does anyone know why the sprintf statement always fails? I have similar code like this that has worked before, but this one always fails for some odd reason. How can it cause an access violation?
chilibean
-
May 26th, 2001, 09:42 AM
#5
Frenzied Member
The answer is fairly simple. Here's your problem code again:
Code:
sprintf(row[i],"%s%s \0",tempp,square[i][x]);
Now here's your declaration for the array square:
Code:
char square[numsqu][numsqu];
sqare is a two dimensional array of characters. With no subscripts, it's a pointer to a 2D array. With one subscript it will be a pointer to a 1D array (which can be a string). With two subscripts it will be a single character, a byte value from 0 to 255.
The source of the problem is that you are telling the sprintf() function that you are passing it two strings. You're not, you're passing it a string, tempp, and a character, square[i][x]. This is actually a type error. Type errors are normally trapped by the compiler at compile time, but in this case the compiler doesn't understand the format string that sprintf() takes, so it can't tell that the types are wrong.
You are getting an access violation because you pass the function a character code. The function is treating this as a pointer to a character (a string) but the area of memory at addresses 0-255 is off limits to your application.
So, basically, you need to either pass a character and change your format string, or change what you're passing to the function and leave the format string as it is.
Harry.
"From one thing, know ten thousand things."
-
May 26th, 2001, 10:21 AM
#6
Thread Starter
Member
Is this what you mean?
Code:
sprintf(row[i],"%s%c \0",tempp,square[i][x]);
Thanks for the help,
chilibean
-
May 26th, 2001, 01:20 PM
#7
Frenzied Member
Exactly
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|