Thread: Compiler error due to char array

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    21

    Compiler error due to char array

    Hi,

    I am trying to declare a char array in the middle of my script which captures a big long list of characters. The code I am using for this is below:

    char data[] = DO_GetUniqueString("VIEWSTATE\" value=\"", "\" />\r\n");

    What I intended to do was then get the size of the array using the strlen method and then use a for loop to basically search through the array. The problem is that I am getting a compiler error with the above statement. The error message I am getting is:

    error C2143: syntax error : missing ';' before 'type'

    This suggested to me that I hadn't closed one of the previous lines correctly with the ';' symbol however I have checked this and it isn't the case. It is also interesting to note that if I comment out this line, it compiles without a problem so I presume there is something I haven't done correctly.

    Thanks once again to anyone who can help someone on his learning curve!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't assign the result of a function to an unsized char string [in fact, you can't assign a char array to another char array]. You will need to set a size [or allocate sufficient memory or some such] and then strcpy() into the char array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    Ok I think I'm starting to understand a little better. So if I declare my array at the top with the appropriate size, e.g. char data[19416] and then capture my string using this code:

    szTempVIEWSTATE = (char *)malloc(20000);
    memset(szTempVIEWSTATE, 0, 19999);
    szTempVIEWSTATE = DO_GetUniqueString("VIEWSTATE\" value=\"", "\" />\r\n");

    I can then copy this into my data array using:

    strcpy(data[19416], szTempVIEWSTATE);

    I've done this, and it compiles successfully which lead me to believe I was getting somewhere but when I run the script I get a MIDDLEWARE EXCEPTION ERROR.

    As you can see I've assigned memory to the szTempVIEWSTATE. Am I right in saying that I should then be able to do a for loop on this to allow me to look through it without the need to strcpy it into a char array?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    szTempVIEWSTATE = (char *)malloc(20000);
    memset(szTempVIEWSTATE, 0, 19999);
    First, I would recommend that you use some CONSTANT for these. Second, why are you clearing one byte less than the total buffer?

    Code:
    strcpy(data[19416], szTempVIEWSTATE);
    What are you doing here?
    Which way do you want to copy things?
    You are currently copying to data[19416].
    - is data a char pointer array?
    - is 19416 a valid index?

    MIDDLEWARE EXCEPTION ERROR.
    This is obviously coming from some "Middleware" component that you are combining your code with. If my suggestions above are not leading to something useful, then it may be that you need help from the producers of the middleware - or at the very least tell us what the middleware is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    Hi

    19416 is the length of the string held in szTempVIEWSTATE. I was therefore attempting to copy the string into the char array I defined at the top of my script which i defined as char data[19416]. I wasn't trying to copy it into a specific location within the char data array but rather copy it all in so that there was a character per location. I put 19416 purely to set the length of the array to be sufficient to receive the data I copy in.

    The MIDDLEWARE exception error will disappear as soon as I get the array working correctly i'm fairly certain of that one.

    Thanks for all your help, can't help but feel cant be far away from getting this working.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what you really want, is to copy 19416 chars from your szViewState variable, is that correct?

    You could use:
    memcpy(szViewState, data, 19416)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    Right I'm that close I can almost touch it.

    To get all of the characters from szTempVIEWSTATE into my char data array I did the following:

    strcpy(data, szTempVIEWSTATE);

    I know this has worked as I can print out characters from the array now so I have the ability to extract the characters one at a time which is brilliant.

    So, here comes the next part. What I planned to do was to have a variable which was:

    char *szVIEWSTATE

    And then what I would do is take every character one at a time from my char data array, check if it was a certain character and if it was append its conversion to szVIEWSTATE, if not I'd append the current value. I'm getting an error now but I'm 99% certain its because I'm not using the strcat incorrectly (i.e. its a middleware error which seems to occur when i've done some wrong coding). Could you please take a look at my code and tell me what i'm doing wrong. I'm sure I am starting to see a light at the end of the tunnel.

    Code:
    szLength = strlen(szTempVIEWSTATE);
    	printf("The length is: %d", szLength);
    	free(szTempVIEWSTATE);
    
    	szVIEWSTATE = (char *)malloc(20000);
    	memset(szVIEWSTATE, 0, 20000);
    
    	for(i=0; i<szLength; i++)
    	{
    		if(data[i] = "+")
    		{
    			strcat(szVIEWSTATE, "%2B");
    		}
    
    		else if(data[i] = "=")
    		{
    			strcat(szVIEWSTATE, "%3D");
    		}
    
    		else if(data[i] = " ")
    		{
    			break;
    		}
    
    		else
    		{
    			strcat(szVIEWSTATE, data[i]);
    		}
    
    		i++;
    	}

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if(data[i] = "+")
    This is ABSOLUTELY wrong if your above strcpy() is right. You are:
    1. Assigning into data (use == to compare()
    2. It's a pointer to a string {'+', '\0'} - the address of that is what is stored [after cropping it to char size] in the array - and it's probably not zero before or after assigning, so "always true".

    Code:
    strcat(szVIEWSTATE, data[i]);
    You can't "strcat" one char. If you track the lenght [remember when you strcat those %xx numbers that they are 3 long], you can do:
    Code:
    szVIEWSTATE[len] = data[i]; 
    len++;

    You really need to enable [or pay attention to] warnings in your compile process. All of the above should have given off at least warnings, possibly errors.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    I think the problem is that I'm using a program which simply uses the compiler from Visual Studio to compile its scripts and therefore doesn't provide me with the error messages that a better development tool may do. I'm really sorry to be a pain but could you explain the very last bit of your post to me. I thought it had been previously said that you couldn't modify the length of an array? It looks below that your adding 1 to the length of the array every time or are you just moving the pointer one along?

    All I want to be able to do is take a character from a char array, compare it, and if its not one of the characters I'm looking for I want to add it a new char array. I'm starting to go crazy with this!

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Godders_2k View Post
    I think the problem is that I'm using a program which simply uses the compiler from Visual Studio to compile its scripts and therefore doesn't provide me with the error messages that a better development tool may do.
    The script [hopefully] contains a list of flags that are sent to the compiler. Add "-W3 " to the list of flags, and you should get warnings. Check if there is a "-W0" - whcih means "turn off warnings".
    I'm really sorry to be a pain but could you explain the very last bit of your post to me. I thought it had been previously said that you couldn't modify the length of an array? It looks below that your adding 1 to the length of the array every time or are you just moving the pointer one along?
    Just moving the "pointer" forward. Note that you would need to update the len by 3 in your "%nn" things too.

    All I want to be able to do is take a character from a char array, compare it, and if its not one of the characters I'm looking for I want to add it a new char array. I'm starting to go crazy with this!
    Yes, so use double equal signs (compare instead of assign), and change your "x" to 'x' to make it a single char, rather than a string of one char plus a zero to mark the end.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    I really thought I'd got it working only to find out that what is being copied into data2 char array is complete and utter nonsense. Characters such as 1/4 and german characters and $ symbols etc. I really cannot see any reason why my code won't work as if I do a single version out of the for loop it appears to be working. If anyone can solve this for me I will be the happiest person alive.

    Code:
    j = 0;
    
    	for(i=0; i<szLength; i++)
    	{
    		if(data[i]=='+')
    		{
    			printf("IF");
    			data2[j] = '\%';
    			j++;
    			data2[j] = '2';
    			j++;
    			data2[j] = 'B';
    			j++;
    		}
    
    		else if(data[i]=='=')
    		{
    			printf("ELSEIFONE");
    			data2[j] = '\%';;
    			j++;
    			data2[j] = '3';
    			j++;
    			data2[j] = 'D';
    			j++;
    			printf("%c", data2[j]);
    		}
    
    		else if(data[i]=='/0')
    		{
    			printf("ELSEIFTWO");
    			break;
    		}
    
    		else
    		{
    			data[j] = data[i];
    			j++;
    		}
    
    	}
    
    	for(j=0; j<szLength; j++)
    	{
    		printf("%c", data2[j]);
    	}

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    data[j] = data[i];
    Maybe this line isn't exactly what you wanted... ??

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    What an idiot I am.

    I could have been looking for hours and not seen such a stupid error. Thanks for all your help. It is working like a dream now. If I ever come to Surrey I owe you a drink for all the help!

    Many thanks to everyone else who helped me too.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are welcome. This is is one of the drawbacks of having two variables that look very similar. It would probably better to call the two variables "src" and "dest" to make them "stand out" a bit more [and you also know exactly which you copy which way - unless you do it completely stupid and copy from dest to src! ;-)]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Object - Terminating Compiler
    By $l4xklynx in forum C++ Programming
    Replies: 12
    Last Post: 11-18-2008, 07:29 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM