This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Error while declaring array and for loop in Code Composer Studio 4

Other Parts Discussed in Thread: MSP430G2553

#include<stdio.h>
#include<string.h>
#include "msp430g2553.h"

void dot(unsigned int d);

void main(void)
{
P1DIR |= 0x01;
char a[] = "string to be entered" ;  <--error is : expected a "}"
for(int i = 0;i<sizeof(a);i++)  <-- error is: expected a declaration. I've declared i outside and initialized here.But still                                                            the error persists
{
switch(a[i])
{
case 'a' :
P1OUT = 0x01;
dot(100);
P1OUT = 0x02;
dot(300);
case 'b' :
dot(100);
case 'c' :
dot(100);
case 'd' :
dot(100);
default :
dot(100);
}
}
}


void dot (unsigned int d )
{
int j,k;
for(k=0;k<d;k++)
for(j=0;j<700;j++);
}  <-- warning here is: last line of file ends without a new line

  • venka tesh said:
    char a[] = "string to be entered" ;  <--error is : expected a "}"

    You declared a as a character array, yet initialized it with a string.

    Perhaps you meant:

    char* a = "string to be entered";

    or...

    char a[] = { 's', 't', 'r', 'i', 'n', 'g', ' ', 't', 'o', ' ', 'b', 'e', ' ', 'e', 'n', 't', 'e', 'r', 'e', 'd', '\0' };

    venka tesh said:
    for(int i = 0;i<sizeof(a);i++)  <-- error is: expected a declaration. I've declared i outside and initialized here.But still                                                            the error persists

    How is the compiler supposed to know what the size of "a" is? Since "a" is a technically pointer to some piece of data, you could change the value of "a" and the size could be different.

    I think you'd be better off with something like:

    for(int i = 0;a[i] != '\0';i++)

  • Hello Venka,

    If memory serves, expression-1 as a declaration was not supported until C99.  Our compilers are C89 with support for GCC extensions.  Declare i outside and initialize inside.

    int i;

    for(i=0; ...

    However, looking at what the code is doing, it might be easier to just use something like

    while(a[i] != "\0")

    instead of a for () statement.

**Attention** This is a public forum