-1

How would I get rid of these syntax errors that are not allowing me to compile?

This has the correct structure for what is doing but these errors are preventing me from testing it out.

I am so close to testing this. I did int array and int pointer to fix some of these errors but doing them in the lines with the next errors does not fix them.

It's all the same type of error.

Pointers.c: In function ‘ArrayInitialize’:
Pointers.c:19: error: expected ‘;’ before ‘)’ token
Pointers.c:23: error: expected ‘;’ before ‘)’ token
Pointers.c:25: warning: assignment makes integer from pointer without a cast
Pointers.c: At top level:
Pointers.c:32: error: expected ‘)’ before ‘array’
Pointers.c:44: error: expected ‘)’ before ‘array’
Pointers.c:56: error: expected ‘)’ before ‘array’
Pointers.c:78: error: expected ‘)’ before ‘pointer’

#include <stdio.h>
#include <stdlib.h>

#define SIZE_OF_ARRAY 5

//=============================================================================

    int *IntegerPtr;
    int  ArrayInt[SIZE_OF_ARRAY]; 
    int *ArrayPtr[SIZE_OF_ARRAY];

//----------------------------------------------------------------------------- 

void ArrayInitialize(int *array,int *pointer){

  int i;
  srand(getpid());

  for (i =0, i < SIZE_OF_ARRAY; i++;){

    array[i] = (int)rand();

  for (i =0, i < SIZE_OF_ARRAY; i++;){

        pointer[i] = &array[i];
                                      }
                      }
    }

//-----------------------------------------------------------------------------

void ArrayPrint(ArrayInt array){
 int i;

   for (i =0, int < SIZE_OF_ARRAY; i++;){
    printf("%d : %10d \n",i,array[i]);

 }
printf("\n");
}

//-----------------------------------------------------------------------------

void ArrayPointerPrint(ArrayInt array){
 int i;

   for (i =0, i < SIZE_OF_ARRAY; i++){
    printf("%d : %10d \n",i,pointer[i]);

 }
printf("\n");
}

//-----------------------------------------------------------------------------

void ArrayBubbleSort(ArrayInt array){

  int i;
  int j;
  int temp;

  for( i = (SIZE_OF_ARRAY - 1); i >= 0; i-- )
  {
    for( j = 1; j <= i; j++ )
    {
      if( *(array+(j-1)) > *(array+j))
      {
         temp = *array+(j-1));
        *array+(j-1)) = array+(j));
        *array+(j) = temp;
      }
    }
  }
}

//-----------------------------------------------------------------------------

 void PointerBubbleSort(ArrayPtr pointer){

  int i;
  int j;
  int temp;

  for( i = (SIZE_OF_ARRAY - 1); i >= 0; i-- )
  {
    for( j = 1; j <= i; j++ )
    {
      if( *(pointer+(j-1)) > *(pointer+j))
      {
        temp = *pointer+(j-1));
        *pointer+(j-1)) = pointer+(j));
        *pointer+(j) = temp;
      }
    }
  }
}

//----------------------------------------------------------------------------- 

 int main(void) {

    int array[SIZE_OF_ARRAY]; 

    int pointer[SIZE_OF_ARRAY];

    ArrayInitialize(array,pointer);

    ArrayPrint(array);

    PointerBubbleSort(pointer);

    ArrayPointerPrint(pointer);

    ArrayBubbleSort(array);

    ArrayPrint(array);

    ArrayPointerPrint(pointer);

    return(EXIT_SUCCESS);

  }

2 Answers2

0

Two of your problems are:

for (i =0, i < SIZE_OF_ARRAY; i++;){

    array[i] = (int)rand();

for (i =0, i < SIZE_OF_ARRAY; i++;){

In both the loops, the comma should be a semicolon and the final semicolon should be missing. As it is, you have an assignment (i = 0 followed by an unused comparison i < SIZE_OF_ARRAY) and the loop condition is i++ != 0 (which takes a long time to become false; you've probably crashed long before it does become false), and there is no reinitialize step. You also don't really want two nested loops both indexing on i — use i and j, instead. Thus, if you need nested loops, you write:

for (i = 0; i < SIZE_OF_ARRAY; i++)
{
    array[i] = (int)rand();
    for (i = 0; j < SIZE_OF_ARRAY; j++)
    {

However, it looks to me like there's no need for nested loops at all:

for (i = 0; i < SIZE_OF_ARRAY; i++)
{
    array[i] = rand();
    pointer[i] = &array[i]; // More on this in a moment
}

The compiler is also diagnosing a problem with the assignment:

pointer[i] = &array[i];

You're assigning a pointer (&array[i]) to an int (because int *pointer means that pointer is a pointer to an int so pointer[i], equivalent to *(pointer + i), is an int). Judging from the code, it looks like in main() you should be declaring:

int *pointer[SIZE_OF_ARRAY];

and the function interface should be:

void ArrayInitialize(int *array, int **pointer)

but all sorts of other parts of the code are inconsistent with that. I'm sure it can be made consistent; the question is which way to make it consistent. There isn't a reason to use pointer at all (that I can see) unless it is int **pointer in the function.

You have another variant of the loop syntax problems at:

for (i =0, int < SIZE_OF_ARRAY; i++;){

That too should be:

for (i = 0; i < SIZE_OF_ARRAY; i++){

In the other functions, you are using the names ArrayInt and ArrayPtr as if they are typedef names, but they're actually (unused) global variables. If you make them into typedefs after all, use them consistently. If you aren't going to make them into typedefs, then get rid of them and fix the function definitions.

You have unmatched parentheses in the sort functions where you swap:

     temp = *array+(j-1));
    *array+(j-1)) = array+(j));
    *array+(j) = temp;

The notation you're using is weird; why not just use array indexes?

temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;

Working code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define SIZE_OF_ARRAY 5

typedef int *IntegerPtr;
typedef int  ArrayInt[SIZE_OF_ARRAY];
typedef int *ArrayPtr[SIZE_OF_ARRAY];

void PointerBubbleSort(ArrayPtr pointer);
void ArrayBubbleSort(ArrayInt array);
void ArrayPointerPrint(ArrayPtr pointer);
void ArrayPrint(ArrayInt array);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer);

void ArrayInitialize(ArrayInt array, ArrayPtr pointer)
{
    int i;
    srand(getpid());

    for (i = 0; i < SIZE_OF_ARRAY; i++)
    {
        array[i] = (int)rand();
        pointer[i] = &array[i];
    }
}

void ArrayPrint(ArrayInt array)
{
    int i;

    for (i = 0; i < SIZE_OF_ARRAY; i++)
    {
        printf("%d : %10d \n", i, array[i]);
    }
    printf("\n");
}

void ArrayPointerPrint(ArrayPtr pointer)
{
    int i;

    for (i = 0; i < SIZE_OF_ARRAY; i++)
    {
        printf("%d : %10d \n", i, *pointer[i]);
    }
    printf("\n");
}

void ArrayBubbleSort(ArrayInt array)
{
    int i;
    int j;
    int temp;

    for (i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
    {
        for (j = 1; j <= i; j++)
        {
            if (array[j-1] > array[j])
            {
                temp = array[j-1];
                array[j-1] = array[j];
                array[j] = temp;
            }
        }
    }
}

void PointerBubbleSort(ArrayPtr pointer)
{
    int i;
    int j;
    int *temp;

    for (i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
    {
        for (j = 1; j <= i; j++)
        {
            if (*pointer[j-1] > *pointer[j])
            {
                temp = pointer[j-1];
                pointer[j-1] = pointer[j];
                pointer[j] = temp;
            }
        }
    }
}

int main(void)
{
    ArrayInt array;
    ArrayPtr pointer;

    ArrayInitialize(array, pointer);
    ArrayPrint(array);

    PointerBubbleSort(pointer);
    ArrayPointerPrint(pointer);

    ArrayBubbleSort(array);
    ArrayPrint(array);

    return(EXIT_SUCCESS);
}

Sample output

0 :  881325466 
1 : 1242393703 
2 :  927466540 
3 : 1493827854 
4 :  533425101 

0 :  533425101 
1 :  881325466 
2 :  927466540 
3 : 1242393703 
4 : 1493827854 

0 :  533425101 
1 :  881325466 
2 :  927466540 
3 : 1242393703 
4 : 1493827854 

Revised code

Minor tweaks to the code to adhere to the letter of the assignment. In particular, it uses the otherwise unused type definition for 'pointer to integer' to build the 'array of five pointers to integer'. It tries to get the output format to match the question's output precisely. Try to avoid trailing spaces on output lines (and source code lines).

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

enum { SIZE_OF_ARRAY = 5 };

typedef int    *IntPtr;
typedef int     ArrayInt[SIZE_OF_ARRAY];
typedef IntPtr  ArrayPtr[SIZE_OF_ARRAY];

void PointerBubbleSort(ArrayPtr pointer);
void ArrayBubbleSort(ArrayInt array);
void ArrayPointerPrint(ArrayPtr pointer);
void ArrayPrint(ArrayInt array);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer);

void ArrayInitialize(ArrayInt array, ArrayPtr pointer)
{
    srand(getpid());

    for (int i = 0; i < SIZE_OF_ARRAY; i++)
    {
        array[i] = rand();
        pointer[i] = &array[i];
    }
}

void ArrayPrint(ArrayInt array)
{
    for (int i = 0; i < SIZE_OF_ARRAY; i++)
        printf("%2d : %10d\n", i, array[i]);
}

void ArrayPointerPrint(ArrayPtr pointer)
{
    for (int i = 0; i < SIZE_OF_ARRAY; i++)
        printf("%2d : %10d\n", i, *pointer[i]);
}

void ArrayBubbleSort(ArrayInt array)
{
    for (int i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
    {
        for (int j = 1; j <= i; j++)
        {
            if (*(array + j-1) > *(array + j))
            {
                int temp = *(array + j-1);
                *(array + j-1) = *(array + j);
                *(array + j) = temp;
            }
        }
    }
}

void PointerBubbleSort(ArrayPtr pointer)
{
    for (int i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
    {
        for (int j = 1; j <= i; j++)
        {
            if (*pointer[j-1] > *pointer[j])
            {
                int *temp = pointer[j-1];
                pointer[j-1] = pointer[j];
                pointer[j] = temp;
            }
        }
    }
}

int main(void)
{
    ArrayInt array;
    ArrayPtr pointer;

    ArrayInitialize(array, pointer);
    puts("---- Initialized array of integers ----");
    ArrayPrint(array);

    PointerBubbleSort(pointer);
    puts("---- Sorted array of pointers ----");
    ArrayPointerPrint(pointer);

    ArrayBubbleSort(array);
    puts("---- Sorted array of integers ----");
    ArrayPrint(array);

    puts("---- Array of pointers ----");
    ArrayPointerPrint(pointer);

    return(EXIT_SUCCESS);
}

Revised output

---- Initialized array of integers ----
 0 :  974520281
 1 : 2052070745
 2 :  565640395
 3 : 1955497143
 4 :  950748713
---- Sorted array of pointers ----
 0 :  565640395
 1 :  950748713
 2 :  974520281
 3 : 1955497143
 4 : 2052070745
---- Sorted array of integers ----
 0 :  565640395
 1 :  950748713
 2 :  974520281
 3 : 1955497143
 4 : 2052070745
---- Array of pointers ----
 0 :  974520281
 1 : 2052070745
 2 :  565640395
 3 : 1955497143
 4 :  950748713
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • This has helped me fix the first problem with the casting with the & symbol. I rather fix these errors of expected ‘)’ before ‘variable' before I get down to that. This problem has been bothering me. – Richy Oliveri Oct 06 '13 at 06:42
  • Got it to compile but it's not running over the terminal for some reason – Richy Oliveri Oct 06 '13 at 07:15
  • Well, on Mac OS X 10.8.5 with GCC 4.8.1, it compiles without any warnings under `gcc -std=c11 -O3 -g -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition`, and it runs as shown. I'm not aware of using any C11 features per se; I'd expect it to compile under `-std=c99` without any changes. In fact, I don't see any C99 features; it should be OK under C89 too. (In fact, GCC 4.6.0 compiles it under both C99 and C89 even with `-pedantic` added). You might have to ensure that the POSIX definitions are enabled, but that's about all I can think of that would affect it. – Jonathan Leffler Oct 06 '13 at 07:24
  • What's incorrect about it? I could reverse the sort criterion for the second sort so that the data is reversed by the second sort, or regenerate the data between sorts, but ... well, I don't see anything wrong with the data in pastebin. It is sorted in ascending order, is it not? – Jonathan Leffler Oct 06 '13 at 18:21
  • http://www.cs.miami.edu/~wuchtys/CSC322-13F/Assessment/LT5.html This is what it should print out – Richy Oliveri Oct 06 '13 at 20:59
  • Silly rules on not using `[]` in the sort function. You're pretty close...The code above deletes the the last `ArrayPointerPrint()` call; it is pretty simple to add that back. The order of the data in the last print block is indeterminate (the example in the question is nearly, but not quite, in descending order, but that's coincidence, I believe). What's the rest of the trouble? You can replace the subscripts `x[n]` with `*(x + n)` in the sort functions to meet the letter of the rules of the assignment, but that is a perfectly pointless restriction on how to code the functions. – Jonathan Leffler Oct 06 '13 at 21:08
  • Since the `PointerBubbleSort()` function is not proscribed from using square brackets, I recommend using subscripts in that function. – Jonathan Leffler Oct 06 '13 at 21:12
0

There are a whole lot of syntax errors in this program, and your code formatting doesn't exactly make it easy to read (for example, the indentation is very inconsistent).

The syntax error gives you the line number it failed on. You should look at that line, and maybe the lines immediately around that line, to determine what went wrong.

For example:

Pointers.c:32: error: expected ‘)’ before ‘array’

line 32:

void ArrayPrint(ArrayInt array){

The error gives you a clue, that something went wrong before array. The problem here is that ArrayInt is used as a type. You haven't defined this type anywhere.

Next, the syntax of your for loop is incorrect. The correct syntax is for (initial condition; condition; increment). Look at that carefully, and compare it to the syntax you used - notice the use of ;s rather than ,s, as well as their placement (See this question if you're interested.

I recommend you fix those, and then look hard at each error, and consult a book, web resource and/or Google to see how you're supposed to implement what you wrote down there (e.g. search for "c for loop syntax".. or "c array tutorial").

Now, if you're struggeling with these, you may want to try using the LLVM clang compiler, though it might be difficult to install if you can't find a linux package for it and/or you're not on a recent Mac OS X version. It outputs some nicer errors:

% clang -o foo foo.c
foo.c:17:9: warning: implicit declaration of function 'getpid' is invalid in C99
      [-Wimplicit-function-declaration]
  srand(getpid());
        ^
foo.c:25:18: warning: incompatible pointer to integer conversion assigning to
      'int' from 'int *'; remove & [-Wint-conversion]
      pointer[i] = &array[i];
                 ^ ~~~~~~~~~
foo.c:23:18: warning: expression result unused [-Wunused-value]
    for (i =0, i < SIZE_OF_ARRAY; i++;){
               ~ ^ ~~~~~~~~~~~~~
foo.c:19:16: warning: expression result unused [-Wunused-value]
  for (i =0, i < SIZE_OF_ARRAY; i++;){
             ~ ^ ~~~~~~~~~~~~~
foo.c:32:17: error: unknown type name 'ArrayInt'
void ArrayPrint(ArrayInt array){
                ^
foo.c:35:12: error: expected ';' in 'for' statement specifier
  for (i =0, int < SIZE_OF_ARRAY; i++;){
           ^
foo.c:35:12: error: expected expression
foo.c:35:38: error: expected ')'
  for (i =0, int < SIZE_OF_ARRAY; i++;){
                                     ^
foo.c:35:7: note: to match this '('
  for (i =0, int < SIZE_OF_ARRAY; i++;){
      ^
foo.c:35:39: error: expected expression
  for (i =0, int < SIZE_OF_ARRAY; i++;){
                                      ^
foo.c:44:24: error: unknown type name 'ArrayInt'
void ArrayPointerPrint(ArrayInt array){
                       ^
foo.c:47:36: error: expected ';' in 'for' statement specifier
  for (i =0, i < SIZE_OF_ARRAY; i++){
                                   ^
foo.c:48:29: error: use of undeclared identifier 'pointer'
    printf("%d : %10d \n",i,pointer[i]);
                            ^
foo.c:47:16: warning: expression result unused [-Wunused-value]
  for (i =0, i < SIZE_OF_ARRAY; i++){
             ~ ^ ~~~~~~~~~~~~~
foo.c:56:22: error: unknown type name 'ArrayInt'
void ArrayBubbleSort(ArrayInt array){
                     ^
foo.c:78:24: error: unknown type name 'ArrayPtr'
void PointerBubbleSort(ArrayPtr pointer){
                       ^
5 warnings and 10 errors generated.

Try to resolve those, and then continue reading. I'll mark the rest as a spoiler so that you can give it a go first.

. . .

Here:

for (i =0, int < SIZE_OF_ARRAY; i++;){

you just randomly put int where i should probably go, once you've fixed your for loop syntax.

void ArrayPointerPrint(int* array){
  int i;

  for (i =0; i < SIZE_OF_ARRAY; i++){
    printf("%d : %10d \n",i,pointer[i]);

Here: what's pointer supposed to be, asks the compiler? It exists neither in the function's scope, nor in the global scope. I guess you meant array.

temp = *array+(j-1);

Why aren't you just using array[j-1] here? While it might work in this particular line, when you try to use the same on the left hand side of the assignment it certainly doesn't. You've also got some extra brackets in this area.

The code below compiles for me. There are still some warnings left though. You should google them.

#include <stdio.h>
#include <stdlib.h>

#define SIZE_OF_ARRAY 5

//=============================================================================

int *IntegerPtr;
int  ArrayInt[SIZE_OF_ARRAY]; 
int *ArrayPtr[SIZE_OF_ARRAY];

//----------------------------------------------------------------------------- 

void ArrayInitialize(int *array,int *pointer){

  int i;
  srand(getpid());

  for (i =0; i < SIZE_OF_ARRAY; i++){

    array[i] = (int)rand();

    for (i =0; i < SIZE_OF_ARRAY; i++){

      pointer[i] = &array[i];
    }
  }
}

//-----------------------------------------------------------------------------

void ArrayPrint(int* array){
  int i;

  for (i =0; i < SIZE_OF_ARRAY; i++){
    printf("%d : %10d \n",i,array[i]);

  }
  printf("\n");
}

//-----------------------------------------------------------------------------

void ArrayPointerPrint(int* array){
  int i;

  for (i =0; i < SIZE_OF_ARRAY; i++){
    printf("%d : %10d \n",i,array[i]);

  }
  printf("\n");
}

//-----------------------------------------------------------------------------

void ArrayBubbleSort(int* array){

  int i;
  int j;
  int temp;

  for( i = (SIZE_OF_ARRAY - 1); i >= 0; i-- )
    {
      for( j = 1; j <= i; j++ )
        {
          if( *(array+(j-1)) > *(array+j))
            {
              temp = *array+(j-1);
          array[j-1] = array[j];
  array[j] = temp;
}
}
}
}

//-----------------------------------------------------------------------------

void PointerBubbleSort(int* pointer){

  int i;
  int j;
  int temp;

  for( i = (SIZE_OF_ARRAY - 1); i >= 0; i-- )
    {
      for( j = 1; j <= i; j++ )
        {
          if( *(pointer+(j-1)) > *(pointer+j))
            {
              temp = *pointer+(j-1);
          pointer[j-1] = pointer+(j);
  pointer[j] = temp;
}
}
}
}

//----------------------------------------------------------------------------- 

int main(void) {

  int array[SIZE_OF_ARRAY]; 

  int pointer[SIZE_OF_ARRAY];

  ArrayInitialize(array,pointer);

  ArrayPrint(array);

  PointerBubbleSort(pointer);

  ArrayPointerPrint(pointer);

  ArrayBubbleSort(array);

  ArrayPrint(array);

  ArrayPointerPrint(pointer);

  return(EXIT_SUCCESS);

}
Community
  • 1
  • 1
m01
  • 9,033
  • 6
  • 32
  • 58
  • The code you supplied above does NOT compile when I overwrite my code. Typing int* array into the function variable declarations gives errors. I am unsure how to correct this error. – Richy Oliveri Oct 06 '13 at 06:42
  • http://www.cs.miami.edu/~wuchtys/CSC322-13F/Assessment/LT5.html The output should be what is on the bottom. – Richy Oliveri Oct 06 '13 at 07:22
  • I just compiled the code with both gcc 4.2.1 and clang 3.2 without errors, so I'd need the compiler error you got to be able to help you. It looks like Jonathan Leffler already solved the problem for you though.. – m01 Oct 06 '13 at 11:11
  • Now your program is working for some odd reason: Here is the output: http://pastebin.com/Mcpjz8F4 – Richy Oliveri Oct 06 '13 at 17:24