0

I am quite new in C so I could not fix it. The code below should take a char array (called Rawpack) and getthesign method should return the 10-19 indexed elements of that array. In python I did it using "return [x:y]" However, as I know there is nothing like that in C. I wanted to write the code that makes same thing as my previous python code but it did not work. What is my mistake? If there is a way to use the python version in C can you enlighten me. Thank you.

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

int main(){
char Sample[] = "nerdesin";
char Checksum[]= "1e052eac";
char Rawpack[]="!neredesin1e052eac#";
char tmpPack[]="";

char ret;
ret=getthesign(Rawpack[])
printf("%c",ret);


}

char getthesign(char package[]){
    char Sign;
  for(int i=10;i<19;i++)
    {
    Sign=(package[i]);
    //printf("%c",Sign);

  }
  return Sign;
  }


char getthepack(char package[]){
 char Pack;
  for(int i=1;i<10;i++)
  {

    Pack=(package[i]);
    //printf("%c",getPack);

  }
  return Pack;
}
  • 1
    You need to return the result in a new string. Either the function has to allocate dynamic memory and return that (caller has to free it) or the function takes an extra `char *` pointer arg provided by the caller for the result to be placed into. In all cases take care to consider buffer sizes and memory leaks. – kaylum Mar 05 '20 at 22:14
  • isn't char is same thing as String in C? it returns a char called pack. – ReduX Completedy Mar 05 '20 at 22:20
  • 2
    No, a `char` is a single character. A C string is one or more characters terminated by a NUL character (`\0`). Suggest you consult a basic C book or tutorial. This will be covered as one of the first things in the strings section. – kaylum Mar 05 '20 at 22:21
  • Ok then How can I return string in my method? inside the for loop as you can see my code get the chars. So how can I turn it to string? – ReduX Completedy Mar 05 '20 at 23:03
  • Did you read the answer in the linked post? It tells you different options for how to obtain an array and then you can fill in that array with the characters you want. – kaylum Mar 05 '20 at 23:06

0 Answers0