I'm trying to obtain the n-th character from a string. However, it's returning me the string from the n-th character onwards.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *test(char input[],int position){
char *result= malloc(sizeof(char)*100);
result=&input[position];
return result;
}
int main(){
char *k;
k=test("abcdefghi\n",3);
printf("%s",k);
}
The above code returns defghi instead of just d. I'm not sure what's wrong here.
CodePudding user response:
You should probably just return a single char
as shown in other answers.
But if you're really required to return char *
, here's how you should do it.
You only need to allocate 2 characters -- one for the character you're returning, and one for the trailing null byte.
You need to copy the character into the allocated memory, not reassign the pointer variable.
char *test(char input[],int position){
char *result= malloc(2);
result[0] = input[position];
result[1] = '\0';
return result;
}
The caller should free the memory after printing it.
int main(){
char *k;
k=test("abcdefghi\n",3);
printf("%s\n",k);
free(k);
}
CodePudding user response:
You're confusing strings and characters. You probably want this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char test(char input[], int position){
return input[position];
}
int main(){
char k = test("abcdefghi\n",3);
printf("%c", k);
}
This will print
d
This is so simple you don't actually need a function. Usually you have a pointer to a string or an array of characters and you just can pick the nth character like this:
char sometext[] = "abcdefghi\n";
...
char k = sometext[3];
CodePudding user response:
I'm trying to obtain the n-th character from a string. However, it's returning me the string from the n-th character onwards.
The function
char *test(char input[],int position){
does not return a character. It returns a pointer.
Moreover the function has a memory leak because at first there is allocated memory and its address is assigned to the pointer result
and then the pointer is reassigned
char *result= malloc(sizeof(char)*100);
result=&input[position];
So the address of the allocated memory is lost.
Apart from this the parameter position
can have a value that exceeds the length of the passed string. So the function can invoke undefined behavior.
If the function returns a pointer to a character then to output the pointed character you need 1) to dereference the pointer and 2) to use the conversion specifier %c
instead of %s
in a call of printf
.
Also as the passed string is not being changed within the function then the corresponding parameter should be declared with the qualifier const
.
The function can be declared and defined the following way
#include <string.h>
#include <stdio.h>
char * test( const char input[], size_t position )
{
char *result = NULL;
if ( position <= strlen( input ) )
{
result = ( char * )( input position );
}
return result;
}
And in main you should write
char *k;
k = test( "abcdefghi\n", 3 );
if ( k != NULL ) printf( "%c\n", *k );
CodePudding user response:
If you want to duplicate the string from the n-th position:
char *copyFronNthPosiztion(const char *input, size_t position)
{
size_t len = strlen(input);
char *result = NULL;
if(position < len)
{
result = malloc(len - position 1);
strcpy(result, input position);
}
return result;
}
If you want to have n-th character you do need any functions. Just use n-th index.
If you want to have a single char string:
#define NTH(str, pos) ((char[]){(str)[pos], 0})
k=NTH("abcdefghi\n",3);
printf("%s",k);