Thread: Visual Studio C++, syntax error : ']'

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Visual Studio C++, syntax error : ']'

    I'm new to arrays, so I'm not sure where the syntax error is coming from. This program is for C, not C++.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void show (const int a[], int size);
    void reverse (int a[], int size);
    
    int main (void)
    {
    	int a[] = {2,4,6,8};
    	show(a, 4);
    	reverse(a, 4);
    	show(a, 4);
    	system("pause");
    }
    
    void show (const int a[], int size)
    {
    	int i;
    	for (i=0; i<size; ++i)
    	{
    		printf("&#37;i%s", a[i], a[i]==a[size-1]?"\n":", ");
    	}
    }
    
    void reverse (int a[], int size)
    {
    	int i;
    	int j=0;
    	int temp[]=a[]; //temporary array for storing old values
    	for (i=size-1; i>=0; --i)
    	{
    		temp[i]=a[j];
    		++j;
    	}
    }
    This problem only occurred after adding the void reverse function.

    The exact error is:
    error C2059: syntax error : ']'

    I'm also wondering if temp[i]=a[j] will permanently change a[]'s values in the main function?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change:
    Code:
    int temp[]=a[];
    to:
    Code:
    int *temp = a;
    After all, a is actually a pointer to the first element of the array that appeared to be passed as an argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you do what lasersight pointed out then yes, a will be changed in main function. When it comes to pointers the data is changed.
    Though it seems to me that you had in mind:
    Code:
    int temp[size];
    strcpy(temp,a); //create copy
    for (...) {
        a[j] = temp[i]
        ...
    }
    which will do what you want. Since you want to change a, you assign to a the values of the temporary array. Note strcpy() to copy arrays since = doesn't work in C.

    At last, keep in mind that you could also do this (save one line of code):
    Code:
    for (i=size-1, j=0; i>=0; --i, ++j)
    or under C99 even this (saving 3 lines of code):
    Code:
    for (int i=size-1, int j=0; i>=0; --i, ++j)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or to
    int* temp = a;

    The problem is that you can't specify empty []. They're used to get a specific index from an array. If you want the entire array, you just specify the name of the array.
    Another problem is that you can't copy C arrays through assignment. And you can't away with not specifying the dimensions of a new array either.

    No, gods, no! Don't use strcpy on an int array. Use memcpy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, strcpy() aside, C_ntua's example relies on variable length arrays, which is a C99 feature.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Hmmm, my instructor didn't mention anything about memcpy talking about arrays. Nonetheless I'll use it if C doesn't allow assignment of arrays by = operator. Meaning

    int *temp = a;

    isn't possible in C from what I understand so memcpy is used... I modified the code to look like:

    Code:
    void reverse (int a[], int size)
    {
    	int i;
    	int j=0;
    	int temp[]; //temporary array for storing old values
    	memcpy(temp,a,size); //Copying old values to temporary array
    	for (i=size-1; i>=0; --i)
    	{
    		a[j]=temp[i];
    		++j;
    	}
    }
    results in error C2133: 'temp' : unknown size

    if I modify it to read

    Code:
    void reverse (int a[], int size)
    {
    	int i;
    	int j=0;
    	int temp[size]; //temporary array for storing old values
    	memcpy(temp,a,size); //Copying old values to temporary array
    	for (i=size-1; i>=0; --i)
    	{
    		a[j]=temp[i];
    		++j;
    	}
    }
    then it results in 2 additional errors:
    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0
    error C2133: 'temp' : unknown size

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    int *temp = a;

    isn't possible in C from what I understand so memcpy is used
    I suggested it because it is possible and is what I thought you wanted to do.

    The deal with memcpy() is when you want to make a copy of the array (and if you are not allowed to use it, your can write your own loop).

    However, the problem is that the array size is not fixed. This means that you either use dynamic memory allocation, or you use a variable length array.

    EDIT:
    Perhaps the problem is that Visual Studio's C compiler has limited support for C99, so a variable length array is not an option. You should use a dynamically allocated array with malloc() and free().
    Last edited by laserlight; 10-14-2008 at 02:02 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by laserlight View Post
    I suggested it because it is possible and is what I thought you wanted to do.

    The deal with memcpy() is when you want to make a copy of the array (and if you are not allowed to use it, your can write your own loop).

    However, the problem is that the array size is not fixed. This means that you either use dynamic memory allocation, or you use a variable length array.
    What does int *temp=a do exactly? It simply fills temp with the values of a, correct?

    Code:
    void reverse (int a[], int size)
    {
    	int i;
    	int j=0;
    	int *temp=a; //temporary array for storing old values
    	for (i=size-1; i>=0; --i)
    	{
    		a[j]=temp[i];
    		++j;
    	}
    }
    Is the closest its been to working. But something is weird because the output from main is:

    2, 4, 6, 8
    8
    6, 6, 8

    when the program is run in its entirety.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No,
    int* temp = a;
    would create a pointer that points to the first element in the array. It's a pointer! You know about pointers, do you not (I hope you do; they're critical in C)?

    And you cannot create arrays of a non-fixed size. In other words, this...
    int temp[];
    ...is illegal.

    And no, you cannot assign an array to another in C.
    Last edited by Elysia; 10-14-2008 at 02:08 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by Elysia View Post
    No,
    int* temp = a;
    would create a pointer that points to the first element in the array. It's a pointer! You know about pointers, do you not (I hope you do; they're critical in C)?
    AHHH, I see what you mean... this isn't what I wanted... I want an identical array. We're just learning about pointers but I'm a bit inattentive at times to stuff like this.

    What I want is if

    a[]={2,4,6,8};

    I want to create an array that mimics that array... i.e.

    temp becomes a[]={2,4,6,8}

    in a sense so that if I made a loop to print each index of temp and a they would be identical.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not an array; it's a pointer.
    It points to the original array - aka it is identical to the original array because when you dereference it, you GET the original array!
    But it's not a copy, if that's what you want.
    For that, you need to malloc enough space and use memcpy (and later on, free it).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by Elysia View Post
    It's not an array; it's a pointer.
    It points to the original array - aka it is identical to the original array because when you dereference it, you GET the original array!
    But it's not a copy, if that's what you want.
    For that, you need to malloc enough space and use memcpy (and later on, free it).
    Ok, the instructor only briefly went over malloc, and he didn't even mention memcpy.


    [edit] - I think he wants us to dereference in this case given the facts.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by Elysia View Post
    It's not an array; it's a pointer.
    It points to the original array - aka it is identical to the original array because when you dereference it, you GET the original array!
    But it's not a copy, if that's what you want.
    For that, you need to malloc enough space and use memcpy (and later on, free it).
    Ok sorry for the double post... I had to wait a bit and get my mind wrapped around the concept... Pointers are generally confusing to me, but I understand it.

    I don't think what I want to do is possible in this fashion because it points to that exact array... So when I change the values of the array the dereference temporary pointer points to those changed values as well...

    I need a copy because I want to preserve old values in array a, so I can then "reverse" those entries. The whole point of the function is to make a copy then overwrite the old array with the old values just in reverse order.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yep, you got the "pointer" part correct.
    That said, unless you can't use malloc, free and memcpy, I suggest you use them to duplicate your array. Plenty of tutorials around, or you could ask if you're uncertain.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by Elysia View Post
    Yep, you got the "pointer" part correct.
    That said, unless you can't use malloc, free and memcpy, I suggest you use them to duplicate your array. Plenty of tutorials around, or you could ask if you're uncertain.
    Making another array is easy enough with a loop, that would then get destroyed I assume because its scope is only inside the reverse function.

    I was just looking for a more efficient way I suppose. I hate throwing more lines of code at stuff if there's a neater/easier/cleaner way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 12:23 PM