char* clean_string (char *input_string){
/*Ensure that input string isn't null and only do heavy lifting if it's not null*/
if (input_string){
char *stripped;
stripped = (char*)malloc(strlen(input_string)*sizeof(char));
while (*input_string != '\0'){
if isalpha(*input_string){
*stripped = toupper(*input_string);
input_string++;
stripped++;
} else {
input_string++;
}
}
/* *stripped++ += '\0';*/
return stripped;
}
/*default return val*/
return NULL;
}
Can anybody tell me where I'm going wrong with this? Tried to do a test run and it doesn't output anything when I try to call it.
You are returning a pointer to the last character in the string (stripped++ ?).
You are allocating one byte too few (should be strlen(...) + 1).
stripped = (char*)malloc(strlen(input_string)*sizeof(char)); /* Wrong. */
stripped = (char*)malloc(strlen(input_string) + 1);
/* .. */
stripped++;
/* .. */
return stripped;
Try to keep a copy, something like original_stripped = stripped before starting to change stripped, and return the copied value (not the incremented one).
The problem is with calling stripped++. You are modifying the pointer you get by malloc. Make an extra pointer char *result_char = stripped; and use that for iteration over resulting string.
The problem ís that you increment your stripped variable before returning it.
Try:
char *stripped;
char *result;
stripped = (char*)malloc(strlen(input_string)*sizeof(char));
result = stripped;
...
return result;
How about just:
char* clean_string (char *input_string)
{
/*Ensure that input string isn't null and only do heavy lifting if it's not null*/
if (input_string)
{
char *stripped;
int i;
stripped = (char*)malloc(strlen(input_string)*sizeof(char) + 1);
for(i=0; i < strlen(input_string); i++)
stripped[i] = (isalpha(input_string[i]) ? toupper(input_string[i]) : input_string[i]);
return stripped;
}
/*default return val*/
return NULL;
}
Related
How do we return a string from a function?
I'm just beginning to learn to use the string functions and malloc, basically, i'm trying to get:
ef = 11101111
as an output.
Here's what i've tried.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int MAXWORD = 2;
char hexToBinary(char hex[MAXWORD]);
int main()
{
char hex[MAXWORD] = {'e','f'};
printf("%s = %s\n", hex, hexToBinary(hex));
return 0;
}
char hexToBinary(char hex[MAXWORD])
{
char *hexToBn = malloc( (MAXWORD-1) * sizeof(char) );
char *convertedString = malloc( (MAXWORD-1) * sizeof(char) );
for(int i=0 ; i<MAXWORD ; ++i)
{
if(hex[i] == 'e' || hex[i] == 'E')
{
strcpy(hexToBn, "1110");
}
if(hex[i] == 'f' || hex[i] == 'F')
{
strcpy(hexToBn, "1111");
}
strcat(convertedString, hexToBn);
}
return convertedString;
}
If you would like to make a function that returns a C string, declare it returning char*:
char *hexToBinary(char hex[MAXWORD]) {
...
}
This is not ideal, because it creates a possibility of a memory leak. In fact, your code would leak a string, because you never free what's allocated in malloc.
You should either fix the leak by capturing the return value and calling free once you are done with it, or use the buffer+length API pattern:
char *bin = hexToBinary(hex);
printf("%s = %s\n", hex, bin);
free(bin);
An alternative API would look like this:
void hexToBinary(char hex[], char bin[], int len) {
... // Users pass the output buffer bin and its length len
}
Couple ways to do so:
1: Allocate string on the heap and pass it to the caller. The caller ensures the string is delete using free.
char * get_str()
{
char * str = malloc(string_length + 1);
// Do something
return str;
}
2: pass the string to the function
void update_string(char * input, int length)
{
// modify string
}
Declare the return type as char*. Then use it like you would.
Note: you may have to do other things to your code. I just said the first thing I saw at a glance. Also, you need to make sure that your memory is deallocated with free when you are done with it.
I have a function written in C that returns either a pointer to a value or null;
char* function (char const *source char const *chars)
{
int size();
int sizeSource = size(source);
int charSource = size(chars);
for (int i = 0; i < sizeSource; i++)
{
for (int j = 0; j < charSource; j++)
{
if (*(source + i) == *(chars + j))
{
return *(source + i);
}
}
}
return "NULL";
}
The function works just fine and I am calling it in this manner:
printf("%c\n", function("ABCDEFG", "HJI"));
printf("%c\n", function("ABCDEFG", "ABC"));
So just to explain here, the first print statement is returning null since HJI is not in ABC and the second returns a pointer to A which is fine. My question is when I currently run it the first print statement just prints nothing, is there some sort of way I can make it print "NULL" if it comes back NULL? I'd rather not have to check each return value for NULL before I print it.
edit: I would like to keep it consistent between success and failure cases. If I change %c to %s in success cases that return a pointer I get a segmentation fault. I also would like to return something not print output in the function.
change %c to %s and return the text string "NULL".
Something like:
static void print_char_or_null(const char *s)
{
if (s) { /* you can use "if (s != NULL)" if you prefer */
putc(*s, stdout);
} else {
printf("NULL");
}
}
And change function to return NULL (not "NULL") in case of failure.
Then you can do:
print_char_or_null(function("ABCDEFG", "HJI"));
print_char_or_null(function("ABCDEFG", "ABC"));
Something ugly:
#define PRINT_CHAR_OR_NULL(pc) \
printf((pc) ?"%c" :"%s", (pc) ?*(pc) :"NULL")
...
char * p = <assign as per OP's requirements>
PRINT_CHAR_OR_NULL(p);
To use this instead of "NULL" return NULL.
I have a fucntion which in it I want to return a string (i.e array of chars) with no spaces at all. This is my code, which in my understanding is not right:
char *ignoreSpace( char helpArr[], int length ){
int i = 0; int j = 0;
char withoutSpace[length];
while ( i < length ){
/*if not a space*/
if ( isspace( helpArr[i] ) == FALSE )
withoutSpace[j] = helpArr[i];
i++;
}
return *withoutSpace;
}
My intention in the line:
return *withoutSpace;
Is to return the content of the array withoutSpace so I could parse a string with no spaces at all.
Can you please tell me how can I make it any better?
Your current solution will lose the result of withoutSpace when the function returns as it is only defined in that function's scope.
A better pattern would be to accept a third argument to the function which is a pointer to a char[] to write the result into - in much the same way the standard functions do, (eg strcpy.
char* ignoreSpace(char* src, char* dst, int length) {
// copy from src to dst, ignoring spaces
// ...
// ...
return dst;
}
Try this (assuming null terminated string)
void ignoreSpace(char *str) {
int write_pos = 0, read_pos = 0;
for (; str[read_pos]; ++read_pos) {
if (!isspace(str[read_pos]) {
str[write_pos++] = str[read_pos];
}
}
str[write_pos] = 0;
}
You cannot return a pointer to a local variable from a function, because as soon as you leave the function all local variables are detroyed and no longer valid.
You must either
Allocate space with malloc in your function and return a pointer
to that allocated memory
not return a pointer from the function butmodify directly the
original string.
First solution :
char *ignoreSpace(char helpArr[], int length)
{
int i=0; int j=0;
char *withoutSpace = malloc(length) ;
while(i <= length)
{
/*if not a space*/
if(isspace(helpArr[i]) == FALSE)
withoutSpace[j++] = helpArr[i];
i++;
}
return withoutSpace;
}
Second solution:
char *ignoreSpace(char helpArr[], int length)
{
int i=0; int j=0;
while(i <= length)
{
/*if not a space*/
if(isspace(helpArr[i]) == FALSE)
helpArr[j++] = helpArr[i];
i++;
}
return helpArr;
}
There are some other small correction in my code. Finding out which ones is left as an exercise to the reader.
You don't increment j, ever. In the case that the current character of the source string is not a space, you probably would like to store it in your output string and then also increment the j by one; so that you'd store the next possible character into the next slot instead of overwriting the 0th one again and again.
So change this:
...
withoutSpace[j] = helpArr[i];
...
into this:
...
withoutSpace[j++] = helpArr[i];
...
And then also append your withoutSpace with a 0 or '\0' (they are the same), so that any string processing function may know its end. Also return the pointer, since you should do that, not the *withoutSpace or withoutSpace[0] (they are the same):
char *ignoreSpace( char helpArr[], int length ){
int i = 0; int j = 0;
char * withoutSpace = malloc( length * sizeof * withoutSpace ); // <-- changed this
while ( i < length ){
/*if not a space*/
if ( isspace( helpArr[i] ) == FALSE )
withoutSpace[j++] = helpArr[i]; // <-- replaced j with j++
i++;
}
withoutSpace[j] = 0; // <-- added this
return withoutSpace;
}
And then you should be good to go, assuming that you can have variable-length arrays.
Edit: Well, variable-length arrays or not, you better just use dynamic memory allocation by using malloc or calloc or something, because else, as per comments, you'd be returning a local pointer variable. Of course, this requires you to manually free the allocated memory in the end.
I'm trying to make a quick function that gets a word/argument in a string by its number:
char* arg(char* S, int Num) {
char* Return = "";
int Spaces = 0;
int i = 0;
for (i; i<strlen(S); i++) {
if (S[i] == ' ') {
Spaces++;
}
else if (Spaces == Num) {
//Want to append S[i] to Return here.
}
else if (Spaces > Num) {
return Return;
}
}
printf("%s-\n", Return);
return Return;
}
I can't find a way to put the characters into Return. I have found lots of posts that suggest strcat() or tricks with pointers, but every one segfaults. I've also seen people saying that malloc() should be used, but I'm not sure of how I'd used it in a loop like this.
I will not claim to understand what it is that you're trying to do, but your code has two problems:
You're assigning a read-only string to Return; that string will be in your
binary's data section, which is read-only, and if you try to modify it you will get a segfault.
Your for loop is O(n^2), because strlen() is O(n)
There are several different ways of solving the "how to return a string" problem. You can, for example:
Use malloc() / calloc() to allocate a new string, as has been suggested
Use asprintf(), which is similar but gives you formatting if you need
Pass an output string (and its maximum size) as a parameter to the function
The first two require the calling function to free() the returned value. The third allows the caller to decide how to allocate the string (stack or heap), but requires some sort of contract about the minumum size needed for the output string.
In your code, when the function returns, then Return will be gone as well, so this behavior is undefined. It might work, but you should never rely on it.
Typically in C, you'd want to pass the "return" string as an argument instead, so that you don't have to free it all the time. Both require a local variable on the caller's side, but malloc'ing it will require an additional call to free the allocated memory and is also more expensive than simply passing a pointer to a local variable.
As for appending to the string, just use array notation (keep track of the current char/index) and don't forget to add a null character at the end.
Example:
int arg(char* ptr, char* S, int Num) {
int i, Spaces = 0, cur = 0;
for (i=0; i<strlen(S); i++) {
if (S[i] == ' ') {
Spaces++;
}
else if (Spaces == Num) {
ptr[cur++] = S[i]; // append char
}
else if (Spaces > Num) {
ptr[cur] = '\0'; // insert null char
return 0; // returns 0 on success
}
}
ptr[cur] = '\0'; // insert null char
return (cur > 0 ? 0 : -1); // returns 0 on success, -1 on error
}
Then invoke it like so:
char myArg[50];
if (arg(myArg, "this is an example", 3) == 0) {
printf("arg is %s\n", myArg);
} else {
// arg not found
}
Just make sure you don't overflow ptr (e.g.: by passing its size and adding a check in the function).
There are numbers of ways you could improve your code, but let's just start by making it meet the standard. ;-)
P.S.: Don't malloc unless you need to. And in that case you don't.
char * Return; //by the way horrible name for a variable.
Return = malloc(<some size>);
......
......
*(Return + index) = *(S+i);
You can't assign anything to a string literal such as "".
You may want to use your loop to determine the offsets of the start of the word in your string that you're looking for. Then find its length by continuing through the string until you encounter the end or another space. Then, you can malloc an array of chars with size equal to the size of the offset+1 (For the null terminator.) Finally, copy the substring into this new buffer and return it.
Also, as mentioned above, you may want to remove the strlen call from the loop - most compilers will optimize it out but it is indeed a linear operation for every character in the array, making the loop O(n**2).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *arg(const char *S, unsigned int Num) {
char *Return = "";
const char *top, *p;
unsigned int Spaces = 0;
int i = 0;
Return=(char*)malloc(sizeof(char));
*Return = '\0';
if(S == NULL || *S=='\0') return Return;
p=top=S;
while(Spaces != Num){
if(NULL!=(p=strchr(top, ' '))){
++Spaces;
top=++p;
} else {
break;
}
}
if(Spaces < Num) return Return;
if(NULL!=(p=strchr(top, ' '))){
int len = p - top;
Return=(char*)realloc(Return, sizeof(char)*(len+1));
strncpy(Return, top, len);
Return[len]='\0';
} else {
free(Return);
Return=strdup(top);
}
//printf("%s-\n", Return);
return Return;
}
int main(){
char *word;
word=arg("make a quick function", 2);//quick
printf("\"%s\"\n", word);
free(word);
return 0;
}
I am trying to write a function to get a string from uart. Its for an embedded system so I don't want to use malloc.
The pointer that is passed to the getstring function seems to point to garbage after the gets_e_uart1() is called. I don't use pointers too often so I'm sure it is something really stupid and trivial that Im doing wrong.
int main()
{
char *ptr = 0;
while(1)
{
gets_e_uart1(ptr, 100);
puts_uart1(ptr);
}
return 0;
}*end main*/
//-------------------------------------------------------------------------
//gets a string and echos it
//returns 0 if there is no error
char getstring_e_uart1(char *stringPtr_, const int SIZE_)
{
char buffer_[SIZE_];
stringPtr_ = buffer_;
int start_ = 0, end_ = SIZE_ - 1;
char errorflag = 0;
/*keep geting chars until newline char recieved*/
while((buffer_[start_++] = getchar_uart1())!= 0x0D)
{
putchar_uart1(buffer_[start_]);//echo it
/*check for end of buffer
wraparound if neccesary*/
if(start_ == end_)
{
start_ = 0;
errorflag = 1;
}
}
putchar_uart1('\n');
putchar_uart1('\r');
/*check for end of buffer
wraparound if neccesary*/
if(start_ == end_)
{
buffer_[0] = '\0';
errorflag = 1;
}
else
{
buffer_[start_++] = '\0';
}
return errorflag;
}
Update:
I decided to go with approach of passing a pointer an array to the function. This works nicely, thanks to everyone for the informative answers.
Updated Code:
//-------------------------------------------------------------------------
//argument 1 should be a pointer to an array,
//and the second argument should be the size of the array
//gets a string and echos it
//returns 0 if there is no error
char getstring_e_uart1(char *stringPtr_, const int SIZE_)
{
char *startPtr_ = stringPtr_;
char *endPtr_ = startPtr_ + (SIZE_ - 1);
char errorflag = 0;
/*keep geting chars until newline char recieved*/
while((*stringPtr_ = getchar_uart1())!= 0x0D)
{
putchar_uart1(*stringPtr_);//echo it
stringPtr_++;
/*check for end of buffer
wraparound if neccesary*/
if(stringPtr_ == endPtr_)
{
stringPtr_ = startPtr_;
errorflag = 1;
}
}
putchar_uart1('\n');
putchar_uart1('\r');
/*check for end of buffer
wraparound if neccesary*/
if(stringPtr_ == endPtr_)
{
stringPtr_ = startPtr_;
*stringPtr_ = '\0';
errorflag = 1;
}
else
{
*stringPtr_ = '\0';
}
return errorflag;
}
Hint: ptr is pointing to garbage before gets_e_uart1() is called.
You need to pass a pointer to your pointer to gets_e_uart1()
EDIT: Except ... if you're trying to have ptr in main() point to the buffer_ in your function ... you've got another problem. buffer_ is on the stack and is out of scope as soon as the function returns. You would need to malloc() that memory in your function.
char getstring_e_uart1(char **stringPtr_, const int SIZE_)
{
char *buffer_ = (char*) malloc(SIZE_ * sizeof(char));
*stringPtr_ = buffer_;
(Editing again because I wasn't paying attention to your main loop, sorry)
Then you're going to have to free it after calling puts_uart1(ptr) which means you mallocing and freeing constantly in that loop. It seems you have a fixed size for that buffer, why not just create it in main() and pass it into both functions?
In addition to Brian's answer I think you also have a problem here:
char getstring_e_uart1(char *stringPtr_, const int SIZE_)
{
char buffer_[SIZE_];
stringPtr_ = buffer_;
after the function returns buffer is no longer valid but stringPtr_ would still point to it. You could make buffer_ static or allocate it globally
You need to construct the buffer prior to invoking getstring_e_uart1(). The code as written will cause the buffer_ variable to be destroyed when the function exits.
I think you want something more like:
char buffer_[SIZE_];
while(1) {
char* ptr = buffer_;
gets_e_uart1(ptr, sizeof(buffer_));
puts_uart1(ptr);
}
Note: I've done no verification regarding the reuse of the variable buffer_.
You don't need to make the buffer static or global - you just have to have the caller allocate it. The changes are trivial:
int main()
{
while(1)
{
char buffer[100] = { 0 };
gets_e_uart1(buffer, sizeof buffer);
puts_uart1(buffer);
}
return 0;
}
//-------------------------------------------------------------------------
//gets a string and echos it
//returns 0 if there is no error
char getstring_e_uart1(char *buffer_, const int SIZE_)
{
int start_ = 0, end_ = SIZE_ - 1;
char errorflag = 0;
/* ... */