If I want to return an empty char*, I can do this
char* Fun1(void) {
return "";
}
Now imagine the same problem with char**, I want to return an empty array of char*.
Is there a shorter way to write this without using a temporary variable ?
char** Fun2(void) {
char* temp[1] = {""};
return temp;
// return {""}; // syntax error !
}
The goal is to hide the fact that the string can be a NULL pointer.
The equivalent to what you did with the string literal would be something like this:
char const* const* Fun2(void) {
static char const* const tmp[] = {""};
return tmp;
}
But as I said in a comment to the question, you might need to re-think what "empty" means.
You also need to consider what you're going to do if there are cases where the return isn't empty. If the caller is supposed to free the returned pointer, then returning pointers to string literals and/or static arrays is out.
Don't use a temporary variable, as it will be out of scope on return. If you have multiple functions that return the same empty array, they can return pointers to the same 'empty array'.
char* EmptyArray[1] = {""};
char** Fun2(void)
{
return EmptyArray;
}
Also, this makes it easy to detect if the function specifically returned emptiness, by comparing pointers:-
char **arr = Fun2();
if (arr == EmptyArray)
...
Firstly, it is not a good idea to return pointers from functions that have just been allocated. The problem is that the user then has to free that memory, and so you are forcing them to use the same memory management function as you are.
With that in mind, you want to allocate 2 bits of memory here: one for the pointer to a character and another for the character itself.
char** p = malloc(sizeof(*p));
*p = malloc(sizeof(**p));
**p = '\0';
return p;
And then remember the user then has to do:
free(*p);
free(p);
But I will say again, what you are doing is not good programming. You are better off writing functions that accept a null pointer.
the {""} only can be used to initial the variable at its definition. but not as an expression, that is, you can't use it like
char* temp[1];
temp = {""}; // invalid
The first case is fine, a string literal such as "" is safe to return from a function. For the second case, where you need an array of length 1 that contains a pointer to an empty string, you could do some trickery:
char ** array_empty_if_null(const char **array)
{
static char *empty[] = { "" };
return (array == NULL || array[0] == NULL) ? empty : array;
}
I think this matches your description of what you need.
Can't you just return null, because null is empty pointer?
Related
I wish to split a "string" by the character ','.
The string holds a GPS NMEA encoded string, but that is of no matter.
My problem is that sometimes the parameter from the function that processes this char array is empty... Like nothing is in the array.
How should I correctly pass a "char string[]" to a function so that I may operate on a that parameter as I sent it(as a char array, not a char pointer to an array).
I also need to specify that I'm using mikroC for PIC.
Here is my code as of right now:
char* GPS_sateliti;
char CsatInView[] =
"$GPGSV,3,2,11,14,25,170,00,16,57,208,39,18,67,296,40,19,40,246,00*74";
GPS_sateliti = GrupeazaDupaVirgule(CsatInView, 2);
char* GrupeazaDupaVirgule( char deGasit[],int nrVirgule ){
int cVirgule = 1;
char* pch = strtok (deGasit,",");
while (pch != 0)
{
pch = strtok (0, ",");
cVirgule++;
if(nrVirgule == cVirgule){
break;
}
}
return pch;
}
The function that operates on the char array received as a parameter in debug mode, before entering the function the char array is fine, after entering it, it seems to be empty
It may be that I should receive a pointer to an array of chars??
Any sort of advice is welcome.
Thank you
How should I correctly pass a "char string[]" to a function so that I may operate on a that parameter as I sent it(as a char array, not a char pointer to an array).
You can't. A function parameter of an array type always decays as the corresponding pointer type.
There are two idiomatic solutions to this.
1. a sentinel:
The last value in the array is a special value that marks the end. This is done in C with strings. They always end with a \0 character, that is guaranteed not to occur inside the string. The function can search for that character to know where the data ends.
(Note: with this info I have to add I'm not sure what your problem is. If you pass an "empty string", as literally "", the \0 will be there, so you shouldn't have a problem)
2. explicitly passing the size:
instead of just
void foo(int bar[]);
you define a function
void foo(size_t barSize, int bar[]);
The caller knows the size of the array, so it can just pass it along.
With a pointer :
char* arr;
yourFunction(arr);
If you wish to initialize it before :
char* arr = malloc(51 * sizeof(char)); // Allocate a memory place of 50 because strings are null terminated in C
yourFunction(arr);
An other way to allocate memory to an array :
char* arr = calloc(50, sizeof(char)); // Allocate 50 memory place which size if the size of a char
With a string :
char arr[50];
char* ptr = arr;
yourFunction(ptr);
You have to know that it is impossible in C to know the size of an array when using pointer. The only thing you can do is to parse the size of the string as a parameter :
size_t size = 50;
char arr[size];
char* ptr = arr;
yourFunction(ptr, size);
If you wish to understand in detail how pointer works and how to iterate them, may be this post can help you. I think it is very interesting.
Globally, you iterate through an array via a pointer like this :
for ( int i = 0; i < size; i++)
printf("Current pointed value in the array : %c\n", ptr[i]); // or arr[i]
I guess you understand why giving the size of a pointed array as a parameter is important. Sometimes you can avoid using this parameter like this :
for ( int i = 0; i != '\0'; i++) // Because strings are null-terminated in C.
// Do something
I have a struct like this:
typedef struct TEXT {
char *text;
struct TEXT *next;
} TEXT;
and in some function I have something like:
TEXT *line = (TEXT *) malloc(sizeof(TEXT));
line->text = NULL; // was "\0" before
char current = getchar();
while (current != '\n') {
AddChar(&(line->text), current); // Was AddChar(line->text, current);
current = getchar();
}
and the AddChar function was something like this:
void AddChar(char **text, char c) { //was char *text
*text = (char *) realloc(*text, strlen(*text)+2); //was text = ...
char char_array[2] = {c, '\0');
strcat(*text, char_array); //was strcat(text, char_array);
}
And unfortunately, program crashed.
As far as I understand, turns out that strlen can't figure out that if text == NULL, length should be 0...
Anyway, with this AddChar function, everything works:
void AddChar(char **text, char c) {
if (*text == NULL) {
*text = (char *) malloc(2);
(*text)[0] = c;
(*text)[1] = '\0';
}
else {
*text= (char *) realloc(*text, sizeof(*text)+2);
char char_array[2] = { c , '\0' };
strcat(*text, char_array);
}
}
.
.
I also had a problem with
void AddChar(char *text, char c) {
text = "something";
}
not changing line->text, but changing *text to **text fixed that.
Only a NULL initialized pointer or a pointer returned by malloc family functions (malloc, calloc and realloc) can be passed to another malloc family functions. line->text is initialized with a string literal "\0" and therefore line->text can't be passed to realloc function.
Also note that you can't modify a string literal.
One of the problems is that you try to reallocate something you didn't allocate yourself.
The other problem is that you try to reassign a local variable inside the AddChar function and expect it to have any effect on the calling function, which it doesn't.
When passing an argument to a function it's passed by value which means that the value is copied into the local argument variable in the function, that variables local inside the function only, and changing that will not change the original variable used when calling the function. What you need is passing the argument by reference which C does not support, but you can emulate it by using a pointer. In your case by using a pointer to the pointer using the address-of operator &.
As far as I understand, turns out that strlen can't figure out that if text == NULL, length should be 0...
On the other hand, section 7.1.4, paragraph 1 the C standard states that NULL is an invalid value for strlen:
If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.
While we're on the subject of invalid values, strcat will expect that both of it's arguments contain strings. What this means is they must both contain a sequence of characters that terminates at the first '\0' character. You've demonstrated this by adding a '\0' character to the array representing your second argument, but can you prove that there's a '\0' character in the array representing your first argument?
*text = (char *) realloc(*text, strlen(*text)+2);
Null pointer aside, anything of the pattern x = realloc(x, ...); is wrong. For more information on that, see Proper usage of realloc.
*text= (char *) realloc(*text, sizeof(*text)+2);
As for this, it may seem to work for you at first, but I can assure you that it's broken. It is important to realise that you intend to allocate characters, not char *s, so sizeof(*text) (which is sizeof (char *)) is invalid here.
Typically this would allocate between 6 and 10 characters. If you overflow that buffer, the behaviour is undefined. What does that mean? Well, it'd be a paradox to define the undefined behaviour, but typical consequences of undefined behaviour range from "nothing at all" and "it works fine" for some systems to "segfaults" and "heartbleed" for other systems; it's not portable nonetheless.
I also had a problem with
void AddChar(char *text, char c) {
text = "something";
}
not changing line->text, but changing *text to **text fixed that.
I'm curious as to what you think AddChar(NULL, 42); would do, when AddChar is defined like this... Do you think it'd assign the null pointer constant that is NULL to the string literal that is "something"?
C doesn't support pass-by-reference. When you pass an argument (for example, a pointer, such as an array expressions implicitly converted to a pointer), what happens behind the scenes is a new variable is declared (in this case, the argument char *text is a variable) and a copy of that value (in this case, a pointer) is assigned to that variable. This is known as "pass-by-value".
When you pass a pointer, and modify the thing that the pointer is pointing at (e.g. by using *pointer = ... or pointer[x] = ...), you're emulating pass-by-reference; you're modifying an object that the variable refers to, rather than modifying the variable itself.
Ohh, and one more thing, despite the link to the "Proper usage of realloc", you should not cast the return value of malloc (or `realloc) in C.
Disagree with OP's final "Anyway, with this AddChar function, everything works:" as that does not allocate sufficient memory.
Suggest
void AddChar(char **text, char c) {
size_t length = strlen(*text);
*text = realloc(*text, length + 2); // proper size calculation
if (*text == NULL) { // check if successful
abort(-1);
}
(*text)[length] = c;
(*text)[length + 1] = '\0';
}
this is a sample program demonstrating getting a string value from a func
can allocate memory inside called function itself and return
can allocate memory inside calling function and called function just updates it.
i am facing problem with the 2nd way.
is there any workaround?
/*
* main.c
*
* Created on: Sep 6, 2014
* Author: Murtaza
*/
#include<stdio.h>
#include<stdlib.h>
char* someFunc1();
void someFunc2(char* str);
void firstApproach();
void secondApproach();
int main()
{
firstApproach();
printf("\n");
secondApproach();
return 0;
}
char* someFunc1()
{
char *str = (char*)malloc(sizeof(char)*10);
str = "HELLO";
return str;
}
void someFunc2(char* str)
{
str = "Hello";
}
void secondApproach()
{
char *str = (char*)malloc(sizeof(char)*10);
someFunc2(str);
printf(str);
printf("heythere");
}
void firstApproach()
{
char *str;
str=someFunc1();
printf(str);
printf("wassup");
}
Please tell me why the second approach isn't working.
thanks!
my output is:
HELLOwassup
h>heythere
and my expected output should be
HELLOwassup
Helloheythere
void someFunc2(char* str)
{
str="Hello"; //-----the variable str is local to this function, thus it goes out of scope as
// soon as the function returns
}
void secondApproach()
{
char *str=(char*)malloc(sizeof(char)*10);
someFunc2(str);
printf(str); // -------------- here the value inside str is some garbage value.
printf("heythere");
}
CORRECTION :
void someFunc2(char **str )
{
*str = "hello";
}
void secondApproach()
{
char *str=(char*)malloc(sizeof(char)*10);
someFunc2(&str); // pass the address of the string
printf("%s", str);
printf("heythere");
}
Let's take a closer look at someFunc2:
void someFunc2(char* str)
{
/* str is a copy of the pointer that was passed in from secondApproach(). */
str = "Hello";
}
Here, you are passing a pointer by value. Thus, str in someFunc2 is a copy of the original pointer str that was passed in from secondApproach(). someFunc2 tells the copy pointer to point somewhere else, but it leaves the original str pointer alone. The solution is to pass in the address of the str pointer then tell someFunc2 to modify the pointer at that address to point to "hello".
void secondApproach()
{
char* str = (char*) malloc(sizeof(char) * 10);
someFunc2(&str); // Pass in the address of str.
printf("%s", str);
printf("heythere");
}
void someFunc2(char** str)
{
*str = "hello";
}
When you write a string between quotes and use it directly as:
a char* -> it gets created in a mutable place in memory, and gives its address to the pointer variable its assigned to. It is still in memory as long as that variable referencing it is not NULL.
an argument -> it gets created as char* and passed to the function (and treated there as a char*), then automatically disposed from memory (i.e. you can't access it or reference it anymore) ..
So, the only situation when a string is actually mutable is when you assign it to a char[], then it gets created in readonly memory and copied into to the stack .. ( the copy on the stack is what the char[] variable will point to )
If you think about it for some time, you realize that this is one of the benefits of dynamic memory: you can create a char* that is mutable, and since it is just a pointer, you don't need to specify the size of the string it is going to point at. So you can see how useful this would be ..
Also, it's worth to note that in functions:
if you pass a string, the variable in the function itself is treated as a char pointer and most likely would try to modify it, and if it's readonly that would raise a Segmentation Fault. So functions are assuming you know what you're doing ..
if you want a function to be able to modify a string directly (i.e string = "somestring") then pass it a pointer to the actual string, else the modification will only be local to the function. That's because the string is a "pointer to char", so the function can't modify the string as a whole. But that also means that a function can modify the string's characters individually ( obviously, because it has a pointer for characters: the string ). If you pass it, however, a string pointer (char**), the function can modify the string directly (*string = "whatever") and can also modify the characters individually (*string[1] = 'C'). Which choice you need depends entirely on the purpose of the function ..
Now, for your specific example, you can do one of two ..
Using a pointer to a pointer to a char (string pointer)
void someFunc2(char** str)
{
*str = "Hello";
}
Using a pointer to char (string)
void someFunc2(char* str)
{
char *k = "Hello";
for(int i = 0; i < 5; ++i)
str[i] = k[i];
}
This is one of the most controversial and advanced topics in C. No matter what type of C programming you're doing, you have to understand these core concepts ..
Good evening, I have 2 functions and each of them accepts as argument a pointer to char:
char pointer[255];
func1(char* pointer)
{
...
memcpy(pointer,some_char,strlen(something));
return;
}
func2(char* pointer)
{
...
if (pointer==someother_char) exit(0); //FAILs
//also I have
if(pointer==someother_pointer2char); // FAILs
}
Now I've tried strstr,strcmp etc... doesn't work. Wanted to try memcmp but I don't have static len. As I have to compare char* to char and char* to char* I would be needing two solutions right?
So, how to compare these pointers (actually pointees) in shortest possible way?
Thanks.
E D I T
Thanks to wallacer and Code Monkey now for char* to char comparison I use following:
func1(char* ptr){
char someother_char[255];
char *ptr_char = NULL; //I have to strcmp a few values so this is why I initialize it first
...
ptr_char = someother_char;
if (strcmp(ptr,ptr_char) == 0) //gtfo and it does...
...
ptr_char = some2nd;
if(strcmp...
Any suggestions maybe... (hmm external function for comparing?)
Suggestion1(by Code Monkey)
#include <stdio.h>
int main(void) {
char tempchar[255];
tempchar[0] = 'a';
tempchar[1] = 'b';
tempchar[2] = '\0';
char *ptr_char;
ptr_char = &tempchar[0];
printf("%s", ptr_char);
return 0;
}
You need to use strcmp. Not seeing how you tried to use it, this is how you should use it:
char *someother_char = "a";
char *pointer = "a";
if (strcmp(pointer, someother_char) == 0) { // match!
}
else { // not matched
}
to then do the comparison with a char, you have to promote to a char*:
char *someother_char1;
char test = 'a';
char *pointer = "a";
strncpy((char*)test,someother_char1,sizeof(test));
if (strcmp(pointer, someother_char1) == 0) { // match!
}
else { // not matched
}
if you want to use the char array then you have to de-reference:
char char_array[255];
// don't forget to fill your array
// and add a null-terminating char somewhere, such as char_array[255] = '\0';
char *ptr_somechar = &char_array[0];
char *pointer = "a";
if (strcmp(pointer, ptr_somechar) == 0) { // match!
} else { // not matched
}
Well right off the bat, if you want to compare the pointees, you need to dereference them. This means to compare the actual char value, you'll have to call
if (*pointer == someother_char)
However this will only compare the first char in the array, which is probably not what you want to do.
To compare the whole thing strcmp should work
char* someother_str = "hello strstr";
if(strcmp(pointer, someother_str) == 0) {
// do something
}
Make sure your other string is declared as a char*
More info: http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
Edit: as per your comment. comparing char* and char doesn't really make sense. One is a character value, the other is an address in memory. Do do so, you can either dereference the char* or reference the value variable.
char c;
char* ptr;
// dereference ptr
if ( c == *ptr ) {
...
}
// reference the value
if ( &c == ptr ) {
}
The first method checks if the values are the same. The second checks if ptr is in fact pointing to the memory containing c ie. is ptr a pointer to c
Hope that helps
Use function srtncmp no srtcmp.
int res = strncmp(str, "¿Cuál es tu nombre? ", 100);
See the next link
compare strings
Strings are null terminated. When you use such kind of strings, it's not a good idea to mixing with other memory copy functions.
Once you do the memcpy operation, please note that your destination string will not be null terminated.
memcmp is a fast operations. Otherwise yo can simply loop through each character and quit upon finding a difference.
To use strcmp, please make sure that both the strings are null terminated. Otherwise it will lead to some crash.
I suggest you to use string functions like strcmp,strlen, strcpy to deal with strings because for that it's actually implemented.
You can't compare two pointers unless both pointers are referring to same memory location. Pointer is just a address to a memory location. What you really want to do is that, to compare the contents rather than compare the address where it's stored. So please use strcmp but again I warn you make sure that it's null terminated.
I have two functions, one that creates a pointer to a string and another that manipulates it. I somehow am missing something critical, however:
int foo() {
char * mystring; // Create pointer
bar(&mystring); // Pass in address
printf("%s\n", mystring);
return 0; // There's a reason for this
}
int bar(char ** mystring) {
mystring[0] = malloc(strlen(mystring) + 1); // Since malloc will persist even after exiting bar
*mystring = "hahaha"; // Dereference
return 0;
}
Any enlightenment for my addled brain would be greatly appreciated!
C doesn't have strings as first class values; you need to use strcpy() to assign strings.
strcpy(mystring[0], "hahaha");
In addition to the other answers given, note that:
mystring[0]
is the same as
*(mystring + 0)
which is the same as
*mystring
Your malloc allocates the memory and the pointer is written to mystring but it is overwritten by the next line.
The use of malloc is necessary, but this way:
mystring[0] = malloc(strlen(mystring) + 1);
is wrong, since you can't perform strlen on mystring(because it doesn't contain any string yet and because the pointer itself is not initialized). Allocate buffer with the size of your string. for example:
int bar(char ** mystring) {
char* hello = "hahaha";
*mystring = malloc(strlen(hello) + 1);
strcpy(*mystring, hello);
return 0;
}
BTW, you could use the assignment *mystring = "hahaha"; without the malloc since this is a string stored in the data section, and the data will not be lost after returning from the function, but this way it is read-only data and you cannot modify it. The strcpy is there to copy the string to the allocated buffer.