Skip to content
Advertisement

error while returning a char from a function in C [closed]

#define SIZE 1024
char encryptEVEN(char *c)
{
    int i;
    i = 0;
    char enEVEN[SIZE], temp[SIZE];
    while (/*c[i] != NULL &&*/c[i] != '')
    {
        if (i % 2 == 0 || i - 3 == 0)
        {
            enEVEN[i] = c[i];
            i++;
        }
    }
    strcpy(temp, enEVEN);
    return enEVEN;
}

int main()
{
    char charr[SIZE], enEVEN[SIZE];
    strcpy(enEVEN, encryptEVEN(charr));
    printf("%s", enEVEN);
}

hello, I am trying to pass and return a char in C but I keep getting warnings and no output displayed I think the problem is with the return for the function .. can I get some help

Advertisement

Answer

I’m not quite sure, but you probably want this:

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

#define SIZE 1024

void encryptEVEN(const char *c, char *destination)
{
  int i = 0;
  int j = 0;

  while (c[i] != '')
  {
    if (i % 2 == 0 || i - 3 == 0)
    {
      destination[j++] = c[i];
    }

    i++;
  }

  destination[j] = '';             // string terminator
}

int main()
{
  char charr[SIZE], enEVEN[SIZE];

  strcpy(charr, "Hello World!");  // put something meaningful into the source string

  encryptEVEN(charr, enEVEN);
  printf("%sn", enEVEN);
}

The output is:

HlloWrd

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement