0

Hi I am trying to print a reversed array through a void function but my IDE is yelling with one errors : expected expression before ']' token.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>  
void printReverses(char szName[], char szReversedName[]);

int main(void)
{

    char szName[21] = "Perry Scope";
    char szReversedName[21];
    char arrRev = printReverses(szName[], szReversedName[]);
    printf("Name in reverse %s \n", arrRev);
    return 0;
}

void printReverses(char szName[], char szReversedName[])
{
    int i, j, lenName;


    lenName=strlen(szName);
    for(i=0, j=lenName-1; i<lenName; i++, j--)
    {
        szReversedName[i] = szName[j];
    }
    szReversedName[i]=szName[lenName];  //add null termination
    return;
}
7
  • Change char arrRev = printReverses(szName[], szReversedName[]); to printReverses(szName, szReversedName); and printf("Name in reverse %s \n", arrRev); to printf("Name in reverse %s \n", szReversedName); Commented Feb 23, 2018 at 4:42
  • Your printReverses function returns nothing, so arrRev will be null. Commented Feb 23, 2018 at 4:43
  • Then don't ignore the yells of the compiler and read it's error message, it will also tell you which line is causing the error.! Commented Feb 23, 2018 at 4:44
  • @eyllanesc It's a void function Commented Feb 23, 2018 at 4:44
  • @MatthewKerian but he wants that void function assign his return to a char which is not correct. Commented Feb 23, 2018 at 4:45

1 Answer 1

1

This is one way of doing it.Try this

#include <stdio.h>

#include <stdlib.h>
#include <string.h>  
void printReverses(char szName[], char szReversedName[]);

int main(void)
{

  char szName[21] = "Perry Scope";
  char szReversedName[21];
  printReverses(szName, &szReversedName);
  printf("Name in reverse %s \n",szReversedName);
  return 0;
}

void printReverses(char szName[], char *szReversedName)
{
  int i, j, lenName;


lenName=strlen(szName);
for(i=0, j=lenName-1; i<lenName; i++, j--)
{
    szReversedName[i] = szName[j];
}
szReversedName[i]=szName[lenName];  //add null termination

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.