This question already has answers here:
Returning an array using C
(8 answers)
Closed 3 years ago.
I want the function lch() to return a string that can be used outside the function.
This is the code I have written, but it does not seem to work:
char *lch(char *ch,int n){
char c[n];
for(int i=0;i<n;i++){
c[i] = *ch;
}
puts(c); // check output string inside function
return c;
}
char str[100],*p;
main(){
p = lch("a",20);
puts(p); // check output outside function
}
I am confused with strings and how they should be passed to functions.
I want the output string to become the same on both calls to puts().
What should I do?
That is the result of the code above:
aaaaaaaaaaaaaaaaaaaa // inside the function
¢ÞêÊ· // outside the function
First of all, returning locally allocated storage will not work. You have to return dynamically allocated storage, and with a proper size to accommodate the null-terminator:
char *c = malloc(n+1);
/* ... */
/* end of program: */
free(p);
Second, you want to pass a character to your function, not a string:
char *lch(char ch,int n){
/* ... */
c[i] = ch;
/* ... */
p = lch('a', 20);
Third, you have to null-terminate your string:
int i;
for(i=0;i<n;i++){
c[i] = ch;
}
ch[i] = '\0';
puts(c); //check output string inside function
Here's the dynamically-allocated storage approach:
#include <stdlib.h>
#include <stdio.h>
char *lch(char ch,int n){
char *c = malloc(n+1);
int i;
for(i=0;i<n;i++){
c[i] = ch;
}
c[i] = '\0';
puts(c); //check output string inside function
return c;
}
char *p;
int main(void){
p = lch('a',20);
puts(p); //check output outside function
free(p);
return 0;
}
This also fixes the declaration and return type of main (main is supposed to be int main(void) or int main(int argc, char **argv)), removes the unneeded variable str, and adds needed #includes.
The reason your code does not do what you expect it to do is because the string in the function is allocated on the stack, which means that its memory is cleaned as soon as you exit the function.
That means the pointer p points to garbage value after the call to lch().
Also, you can not declare an array of size that is not a constant value, so the line:
char c[n];
would simply not work.
To solve this, you will need to dynamically allocate the string using malloc(3) :
char* lch(char ch, int n)
{
char * c;
/* Allocate n bytes of memory for the string */
c = malloc(n + 1);
if (NULL == c)
{
/* Failed to allocate memory, exit the function */
return c;
}
for(int i = 0; i < n; i++)
{
c[i] = ch;
}
/* Add a terminating null byte (to make it a string) */
c[i] = '\0';
puts(c);
return c;
}
int main(void)
{
char * p;
p = lch('a', 20);
puts(p);
/* Free the string from the memory */
free(p);
return 0;
}
I added a few fixes to the code but the main thing you need to look at is the use of malloc(3).
I dynamically allocated n+1 bytes of memory for the string, then wrote the data into the string (plus a '\0'), and when the function exits the memory will still be available and wont be corrupted.
The call to free is needed to free the memory we have allocated.
You can read more about malloc(3) here: https://linux.die.net/man/3/malloc
Related
This is what I expect my string array s to be after the program is run: {"#0", "#1", "2"}.
This is what I am getting: {"#2", "#2", "2"}.
How do I modify this code so that I can get {"#0", "#1", "#2"} in the main after the function is executed?
What am I doing wrong? Please help.
#include <stdio.h>
void func(char **s){
for(int i=0; i<3; i++){
char buf[10];
snprintf(buf, 10, "#%d",i);
s[i]=buf;
}
}
int main()
{
char *s[3];
func(s);
for(int i=0; i<3; i++){
printf("%s", s[i]);
}
return 0;
}
First, you have every element in your array pointing to the same character array.
Second, they are all pointing to a local character array. This leads to undefined behavior.
Local variables in functions get allocated on the stack. When the function call is over, that memory may be overwritten with some other value.
Given two pointers p and q, the statement:
p = q;
doesn't copy the contents of the memory pointed to by q to the contents of the memory pointed to by p. It copies the pointer values, such that both p and q now point to the same memory, and any change to the memory via p is reflected when q is used.
That being said, the statement:
char buf[10];
declares buf to be an array of 10 chars. It has a lifetime corresponding to the execution of its block of definition. Once the function returns, it's destroyed and s is now indeterminate. Indeterminate pointers lead to undefined behaviour.
Possible Solutions:
standard strcpy
POSIX's strdup (which will be included in C23)
Note that the strdup() function returns a pointer to a new string which is
a duplicate of the provided string. Memory for the new string is obtained with malloc, and must be freed by the calling process with free.
#Chris’s answer tells you what is wrong.
To fix it, you have options. The simplest is to make the argument array have strings (char arrays) that are big enough for your uses:
#define MAX_STR_LEN (9+1) // Every string’s capacity is 9+1 characters
void func(size_t n, char array_of_string[][MAX_STR_LEN])
{
for (size_t i=0; i<n; i++)
{
snprintf(array_of_string[i], MAX_STR_LEN, "#%d", (int)i); // use the extant string
}
}
int main(void)
{
char array_of_string[3][MAX_STR_LEN] = {{0}}; // Array of 3 strings
func(3, array_of_string);
...
return 0;
}
If you want to play with dynamic char * strings, life gets only a little more complicated:
void func(size_t n, char *array_of_string[])
{
for (size_t i=0; i<n; i++)
{
free(array_of_string[i]); // free any pre-existing string
array_of_string[i] = calloc( 10, 1 ); // allocate our new string
if (!array_of_string[i]) fooey(); // always check for failure
snprintf(array_of_string[i], 10, "#%d", (int)i); // use the new string
}
}
int main(void)
{
char *array_of_string[3] = {NULL}; // Array of 3 dynamic strings, all initially NULL
func(3, array_of_string);
...
for (size_t i=0; i<3; i++) // Don’t forget to clean up after yourself
free(array_of_string[i]);
return 0;
}
Ultimately the trick is to manage the size of your strings, remembering that a string is itself just an array of char. You must ensure that there is enough room in your character array to store all the characters you wish. (Good job on using snprintf()!
Also remember that in C any argument of the form array[] is the same as *array. So our functions could have been written:
void func(size_t n, char (*array_of_string)[MAX_STR_LEN])
or
void func(size_t n, char **array_of_string)
respectively. The first is an uglier (harder to read) syntax. The second is nicer, methinks, but YRMV.
Finally, if you are using C99 (or later) you can tell the compiler that those arguments are, actually, arrays:
void func(size_t n, char array_of_string[n][MAX_STR_LEN])
or
void func(size_t n, char *array_of_string[n])
MSVC does not support that syntax, though, and probably never will, alas.
{ // start of a new scope
char buf[10]; // a variable with automatic storage duration
// ...
} // end of scope - all automatic variables end their life
In your code, you make pointers point at buf which has seized to exist (3 times) at the } in the for loop. Dereferencing (reading from the memory those pointers point at) those pointers afterwards makes your program have undefined behavior (anything could happen).
What you can do is to allocate and release memory dynamically using malloc and free.
When sending in a pointer to the first element in an array of elements to a function like you do, it's also customary to provide the length of the array (the number of elements in the array) to the function.
It could look like this:
#include <stdio.h>
#include <stdlib.h>
// A macro to calculate the number of elements in an array
// sizeof (x) - the number of bytes the whole array occupies
// sizeof *(x) - the size of the first element in the array
// (all elements have equal size)
// The result of the division is the number of elements in the array.
#define SIZE(x) (sizeof (x) / sizeof *(x))
void func(char *s[], size_t len) {
for (size_t i = 0; i < len; i++) {
// calculate the required length for this string
size_t req = snprintf(NULL, 0, "#%zu", i) + 1; // +1 for '\0'
// and allocate memory for it
s[i] = malloc(req);
if(s[i] == NULL) exit(1); // failed to allocate memory
snprintf(s[i], req, "#%zu", i);
} // dynamically allocated memory is _not_ released at the end of the scope
}
int main() {
char *s[3];
func(s, SIZE(s));
for (size_t i = 0; i < SIZE(s); i++) {
puts(s[i]);
free(s[i]); // free what you've malloc'ed when you are done with it
}
}
Note that with the use of the macro there is only one hardcoded 3 in the program. Even that could be made into a named constant (#define CHAR_PTRS (3) or enum { CHAR_PTRS = 3 };) to further the ease of reading and maintaining the code.
A non-idiomatic version only accepting a pointer to an array of a fixed (at compile time) size could look like like below. In this example you couldn't accidentally provide a pointer to an array with only 2 char* (which would cause the function to write out of bounds). Instead, it'd result in a compilation error.
#include <stdio.h>
#include <stdlib.h>
// Here `s` is a pointer to an array of 3 char*
void func(char *(*s)[3]) {
for (int i = 0; i < 3; i++) {
(*s)[i] = malloc(10);
if((*s)[i] == NULL) exit(1);
snprintf((*s)[i], 10, "#%d", i);
}
}
int main() {
char *s[3];
func(&s); // &s is a char*(*)[3]
for (int i = 0; i < 3; i++) {
printf("%s\n", s[i]);
free(s[i]);
}
}
#include <stdio.h>
#include <string.h>
void func(char **s){
for(int i=0; i<3; i++){
s[i]=malloc(sizeof(char) * 100);
char buf[10];
snprintf(buf, 10, "#%d",i);
strcpy(s[i], buf);
}
}
int main()
{
char *s[3];
func(s);
for(int i=0; i<3; i++){
printf("%s", s[i]);
}
return 0;
}
This fixed my problem. My understanding is that I assigned memory and then copied the contents of buf to s to the now-present memory.
I have Wrote the Following Code Snippet but it is not working.
Can anyone help me? Where i am Wrong ??
char *xstrrev(char *string)
{
int len=0,i=0;
char *reverse;
while(*string!='\0')
{
len++;
string++;
}
len=len-1;
while(len>=0)
{
*reverse=string[len];
reverse++;
len--;
}
*reverse='\0';
return reverse;
}
int main()
{
char name[10];
scanf("%s",name);
printf("%s",xstrrev(name));
return 0;
}
I am unable to return whole string in the main function
The problem starts (and ends) with
*reverse=string[len];
where reverse is not initialized. This invokes undefined behavior.
You need to initialize reverse to make it point to a valid memory location before you can dereference the pointer.
Since you are expecting to _return_ the new _string_ from your function and use it in the caller, you should use memory allocator functions, like malloc() to allocate memory and initialize the reverse with the returned pointer, after the success check of the malloc() call. You also need to take care of free()-ing the allocated memory , once you're done using it.
After that, as per your logic, you're doing reverse++; and in the end, you're returning reverse, so think of the exact value which is getting returned. You're returning a pointer to the end of the string, not a pointer to the start of it. You need to keep a copy of the actual start of reverse and return that.
Three problems:
[1] reverse not pointing to valid memory area.
[2] returning reverse will return the last byte address of string reverse. so need to store the start of reverse and return the start of reverse.
[3] string is completely parsed while calculating the length, so again, start of original string needs to be saved and used subsequently
char *xstrrev(char *string)
{
char *original_string = string;
int len=0,i=0;
//[1] char *reverse;
while(*string!='\0')
{
len++;
string++;
}
//[3] till here "string" is completely parsed, so it points to past the end.
char *reverse = (char*)malloc(len + 1);
char *reverse_to_be_returned = reverse;
len=len-1;
while(len>=0)
{
//[3] *reverse=string[len];
*reverse = original_string[len];
reverse++;
len--;
}
*reverse='\0';
//[2] return reverse;
return reverse_to_be_returned;
}
You need to allocate space for char *reverse to point to. This can be done with malloc or strdup.
Basically:
malloc()allocates requested memory on the heap, and returns a void* pointer in the end.
You also need to check the return pointer of any call to malloc(), as sometimes it can return NULL if something wrong happens.
You also need to free this requested memory at the end, just to be safe.
Additionally, since you are returning a pointer, you need to make sure it points at the start of the string, instead of the end.
Your code can look like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAMESTRLEN 10
char *xstrrev(char *string);
int
main(void) {
char name[NAMESTRLEN+1];
char *result;
printf("Enter string: ");
scanf("%10s", name);
result = xstrrev(name);
printf("Reversed string = %s\n", result);
free(result);
return 0;
}
char
*xstrrev(char *string) {
char *result;
size_t slen, count = 0;
int i;
slen = strlen(string);
result = malloc(slen+1); /* +1 for nullbyte */
if (!result) {
printf("Cannot allocate space for string.\n");
exit(EXIT_FAILURE);
}
for (i = slen-1; i >= 0; i--) {
result[count++] = string[i];
}
result[count] = '\0';
return result;
}
So I've looked around on SO and can't find code that answers my question. I have written a function that is supposed to reverse a string as input in cmd-line. Here is the function:
void reverse (char string[]) {
int x;
int i = 0;
char line[strlen(string)];
for (x = strlen(string) - 1; x > 0; x--) {
char tmp = string[x];
line[i] = tmp;
i++;
}
string = line;
}
When I call my reverse() function, the string stays the same. i.e., 'abc' remains 'abc'
If more info is needed or question is inappropriate, let me know.
Thanks!!
You're declaring your line array one char shorter remember the null at the end.
Another point, it should be for (x = strlen(string) - 1; x >= 0; x--) since you need to copy the character at 0.
void reverse (char string[]) {
int x;
int i = 0;
char line[strlen(string) + 1];
for (x = strlen(string) - 1; x >= 0; x--) {
char tmp = string[x];
line[i] = tmp;
i++;
}
for(x = 0; x < strlen(string); x++)
{
string[x] = line[x];
}
}
Note that this function will cause an apocalypse when passed an empty string or a string literal (as Bobby Sacamano said).
Suggestion you can probably do: void reverse(char source[], char[] dest) and do checks if the source string is empty.
I think that your answer is almost correct. You don't actually need an extra slot for the null character in line. You just need two minor changes:
Change the assignment statement at the bottom of the procedure to a memcpy.
Change the loop condition to <-
So, your correct code is this:
void reverse (char string[]) {
int x;
int i = 0;
char line[strlen(string)];
for (x = strlen(string) - 1; x >= 0; x--) {
char tmp = string[x];
line[i] = tmp;
i++;
}
memcpy(string, line, sizeof(char) * strlen(line));
}
Since you want to reverse a string, you first must decide whether you want to reverse a copy of the string, or reverse the string in-situ (in place). Since you asked about this in 'C' context, assume you mean to change the existing string (reverse the existing string) and make a copy of the string in the calling function if you want to preserve the original.
You will need the string library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Array indexing works, and this version takes that approach,
/* this first version uses array indexing */
char*
streverse_a(char string[])
{
int len; /*how big is your string*/
int ndx; /*because 'i' is hard to search for*/
char tmp; /*hold character to swap*/
if(!string) return(string); /*avoid NULL*/
if( (len=strlen(string)) < 2 ) return(string); /*one and done*/
for( ndx=0; ndx<len/2; ndx++ ) {
tmp=string[ndx];
string[ndx]=string[len-1-ndx];
string[len-1-ndx]=tmp;
}
return(string);
}
But you can do the same with pointers,
/* this is how K&R would write the function with pointers */
char*
streverse(char* sp)
{
int len, ndx; /*how big is your string */
char tmp, *bp, *ep; /*pointers to begin/end, swap temporary*/
if(!sp) return(sp); /*avoid NULL*/
if( (len=strlen(bp=sp)) < 2 ) return(sp); /*one and done*/
for( ep=bp+len-1; bp<ep; bp++, ep-- ) {
tmp=*bp; *bp=*ep; *ep=tmp; /*swap*/
}
return(sp);
}
(No, really, the compiler does not charge less for returning void.)
And because you always test your code,
char s[][100] = {
"", "A", "AB", "ABC", "ABCD", "ABCDE",
"hello, world", "goodbye, cruel world", "pwnz0r3d", "enough"
};
int
main()
{
/* suppose your string is declared as 'a' */
char a[100];
strcpy(a,"reverse string");
/*make a copy of 'a', declared the same as a[]*/
char b[100];
strcpy(b,a);
streverse_a(b);
printf("a:%s, r:%s\n",a,b);
/*duplicate 'a'*/
char *rp = strdup(a);
streverse(rp);
printf("a:%s, r:%s\n",a,rp);
free(rp);
int ndx;
for( ndx=0; ndx<10; ++ndx ) {
/*make a copy of 's', declared the same as s[]*/
char b[100];
strcpy(b,s[ndx]);
streverse_a(b);
printf("s:%s, r:%s\n",s[ndx],b);
/*duplicate 's'*/
char *rp = strdup(s[ndx]);
streverse(rp);
printf("s:%s, r:%s\n",s[ndx],rp);
free(rp);
}
}
The last line in your code does nothing
string = line;
Parameters are passed by value, so if you change their value, that is only local to the function. Pointers are the value of the address of memory they are pointing to. If you want to modify the pointer that the function was passed, you need to take a pointer to that pointer.
Here is a short example of how you could do that.
void reverse (char **string) {
char line = malloc(strlen(*string) + 1);
//automatic arrays are deallocated once the function ends
//so line needs to be dynamically or statically allocated
// do something to line
*string = line;
}
The obvious issue with this is that you can initialize the string with static memory, then this method will replace the static memory with dynamic memory, and then you'll have to free the dynamic memory. There's nothing functionally wrong with that, it's just a bit dangerous, since accidentally freeing the string literal is illegal.
char *test = "hello";
reverse(test);
free(test); //this is pretty scary
Also, if test was allocated as dynamic memory, the pointer to it would be lost and then it would become a memory leak.
Language: C
I am trying to program a C function which uses the header char *strrev2(const char *string) as part of interview preparation, the closest (working) solution is below, however I would like an implementation which does not include malloc... Is this possible? As it returns a character meaning if I use malloc, a free would have to be used within another function.
char *strrev2(const char *string){
int l=strlen(string);
char *r=malloc(l+1);
for(int j=0;j<l;j++){
r[j] = string[l-j-1];
}
r[l] = '\0';
return r;
}
[EDIT] I have already written implementations using a buffer and without the char. Thanks tho!
No - you need a malloc.
Other options are:
Modify the string in-place, but since you have a const char * and you aren't allowed to change the function signature, this is not possible here.
Add a parameter so that the user provides a buffer into which the result is written, but again this is not possible without changing the signature (or using globals, which is a really bad idea).
You may do it this way and let the caller responsible for freeing the memory. Or you can allow the caller to pass in an allocated char buffer, thus the allocation and the free are all done by caller:
void strrev2(const char *string, char* output)
{
// place the reversed string onto 'output' here
}
For caller:
char buffer[100];
char *input = "Hello World";
strrev2(input, buffer);
// the reversed string now in buffer
You could use a static char[1024]; (1024 is an example size), store all strings used in this buffer and return the memory address which contains each string. The following code snippet may contain bugs but will probably give you the idea.
#include <stdio.h>
#include <string.h>
char* strrev2(const char* str)
{
static char buffer[1024];
static int last_access; //Points to leftmost available byte;
//Check if buffer has enough place to store the new string
if( strlen(str) <= (1024 - last_access) )
{
char* return_address = &(buffer[last_access]);
int i;
//FixMe - Make me faster
for( i = 0; i < strlen(str) ; ++i )
{
buffer[last_access++] = str[strlen(str) - 1 - i];
}
buffer[last_access] = 0;
++last_access;
return return_address;
}else
{
return 0;
}
}
int main()
{
char* test1 = "This is a test String";
char* test2 = "George!";
puts(strrev2(test1));
puts(strrev2(test2));
return 0 ;
}
reverse string in place
char *reverse (char *str)
{
register char c, *begin, *end;
begin = end = str;
while (*end != '\0') end ++;
while (begin < --end)
{
c = *begin;
*begin++ = *end;
*end = c;
}
return str;
}
this is the rough idea of what I am trying to do:
I want the pointer in main to point to the word I just in my function.
my actual code is very long so please excuse this format.
main()
{
char *word;
int lim 256;
*word = function(word,lim)//I am not returning the address back only the first letter
}
function(word,lim)
{
//memory allocation
//getting word
//reset address
return(*word);//I am passing the correct address here
}
char* allocate_word(int lim)
{
// malloc returns a "void*" which you cast to "char*" and
//return to the "word" variable in "main()"
// We need to allocate "lim" number of "char"s.
// So we need to multiply the number of "char"s we need by
//the number of bytes each "char" needs which is given by "sizeof(char)".
return (char*)malloc(lim*sizeof(char));
}
int main()
{
char *word;
// You need to use "=" to assign values to variables.
const int lim = 256;
word = allocate_word(lim);
// Deallocate!
free(word);
return 0;
}
Functions used in the sample code above:
malloc
free
This seems like a decent tutorial:
C Tutorial – The functions malloc and free
char* func()
{
return (char*)malloc(256);
}
int main()
{
char* word = func();
free(word);
return 0;
}