Results 1 to 7 of 7

Thread: Arrays of chars

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    57

    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.

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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)

  3. #3
    Zaei
    Guest
    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    57

    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

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    57

    Is this what you mean?

    Code:
    sprintf(row[i],"%s%c \0",tempp,square[i][x]);
    Thanks for the help,

    chilibean

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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
  •  



Click Here to Expand Forum to Full Width