Returning an array from a C-function - c

I'm currently writing an application where I need a C-function to return an array. I've read that C-functions directly can't return arrays but pointers to arrays, anyhow I still don't get it to work. I'm sending a string with several numerical values that I have to put into an array.
My code looks like this, where the main function is:
int main() {
char arr[3] = {0};
char *str = "yaw22test242test232";
foo(arr,3,str);
printf("%d\n",arr[0]);
return 0;
}
Where I want the foo function to return an array with the numbers 22, 242 and 232 on array positions 0, 1 and 2 respectively. The algorithm in the foo function works properly when used in the main program but not this way. Is there any way to work around this? What am I doing wrong? The foo function looks as follows:
void foo(char *buf, int count, char *str) {
char *p = str;
int k = 0;
while (*p) { // While there are more characters to process...
if (isdigit(*p)) { // Upon finding a digit, ...
double val = strtod(p, &p); // Read a number, ...
//printf("%f\n", val); // and print it.
buf[1-count-k] = val;
k++;
} else { // Otherwise, move on to the next character.
p++;
}
}
}

Well you are going out of bounds here:
buf[1-count-k] = val;
perhaps you mean something like buf[k] = val; and a check if( k >= count ) to end the loop.
Since char *buf usually isn't able to represent values larger than 127, you should use an integer type large enough, or a double, otherwise the assignment buf[*] = val; from a type double to a type char, will cause undefined behavior.

It looks like you want to extract the numbers in the string as doubles, but you're trying to store them in a char array. This doesn't even compile.
So, first, use a proper buffer:
int main() {
double arr[3] = {0};
/* ... */
}
And update the parameter declaration in foo():
void foo(double *buf, int count,char *str) { ... }
And then fix this:
buf[1-count-k] = val;
You probably want something as simple as:
buf[k++] = val;
Finally, you may want to return k so that the caller has a chance to know how many numbers were written into the array. So, foo would look like this:
size_t foo(double *buf, int count,char *str) {
char *p = str;
size_t k = 0;
while (*p) { // While there are more characters to process...
if (isdigit(*p)) { // Upon finding a digit, ...
double val = strtod(p, &p); // Read a number, ...
//printf("%f\n", val); // and print it.
buf[k++] = val;
} else { // Otherwise, move on to the next character.
p++;
}
}
return k;
}
Note that the correct type to index an array is size_t, and not int. size_t is guaranteed to be wide enough to hold the size of any array, so if you want your code to work with arbitrarily long arrays, size_t should be used to index the array.

I would recommend using a vector-like structure instead of an array. There are many implementations of this already (see GLib lists for C). But if you want to 'roll your own', try something similar to this:
typedef struct
{
char** data;
int size;
} str_vector;
Where you can dynamically allocate a str_vector and its data member, and return this. I won't go into too much more detail as there is quite a few tutorials on this on the internet, which I am sure you can bring up in Google/Bing/Whatever in a manner of seconds :)

Related

c function convert "fffoootoo" to "foto" (leaves out following repeating characters)

The task would be to remove following characters that are repeating from a char array, like "deeeciddeee" -> "decide" or "phhhonne" -> "phone".
I have a function that crashes the console, and I can't spot the bug:
char* my_unique(char *first, char *last) {
char* ret=first;
for(int i=0; first+i!=last; i++){
if(first[i]==first[i+1]){
for(int j=i; first+j!=last; j++)
first[j]=first[j+1];
last--;
}
}
return ret;
}
it is called this way:
char* a="oooat";
a=my_unique(a, a+strlen(a));
cout<<a;
please help me!
Besides a small bug (you should add the line i--; after last--;, because you're deleting the character at possition i, so what has been the character at i+1 became the new character at possition i. If you don't decrease i, it will be increased and you jump over a character) the code runs perfectly fine IF it is called with
const char* b = "oooat";
char* a = new char[strlen(b) + 1];
for (size_t c = 0; c < strlen(a) + 1; c++) { a[c] = b[c]; }
a = my_unique(a, a + strlen(a));
cout << a;
delete[] a;
Notice that I've used a edit-able copy of the string, as the literal itself is of type const char* and therefor can't be changed at all. And as I said, this works perfectly fine and prints "oat", just as expected, without any crash. So your problem might be that you try to edit a const string literal? In that case you might consider to copy it, as I did, or use std::string (if you code in C++).
There are many beginner mistakes in the code.
Let me point you one by one.
char* a="oooat";
a=my_unique(a, a+strlen(a));
cout<<a;
When you declare a string like this : char* a="oooat", a is a string literal. The memory for the string is allocated into text section of the program. Which basically means you cannot modify the values inside the strings. You can only read from them. Hence when you are passing pointer a to the function and modifying it, it will result in segmentation fault(Illegal access to memory).
Why do you need a ret pointer here? char* ret=first;
You are passing a pointer and modifying the value inside it. Hence the new data will be reflected in the calling function and we need not return it explicitly. So, it is redundant.
Overall logic can be simplified as well
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MYSTR "ooooat"
void my_unique(char *first, char *last) {
int size = last - first;
int i = 0, j = 0, k = 0;
for (; i < size; i++, j++) {
first[j] = first[i];
// Continue to check how many repetitions are there
while (i + 1 < size && (first[i] == first[i+1])) i++;
}
// In the end terminate the string with a NULL.
first[j] = '\0';
return;
}
int main()
{
char a[] = MYSTR;
my_unique(a, a+strlen(a));
printf("%s", a);
return 0;
}
This is in C. There are simpler ways of doing this in C++, and the code can definitely be condensed but has been left simpler for readability.
#include <stdlib.h>
char* fix(char *input) {
char *lookahead = input;
char *newchar, *ret;
// Determine Max Return String Length
int len = 0;
while (*lookahead != '\0') {
len++;
lookahead++;
};
// allocate max possible memory needed and set the pointers
ret = malloc(len);
newchar = ret;
lookahead = input;
*newchar = *lookahead; // copy the first character
while (*lookahead != 0) {
lookahead++; // incrementing this ptr first starts lookahead at 2nd character and
// ensures the null terminator gets copied before the while loop ends
if (*newchar != *lookahead) { // only copy new characters to new return string
newchar++;
*newchar = *lookahead;
};
};
return ret;
};
I'll try to give my answer so that it makes the as little changes as possible to your original code, while using the simplest methods.
The main problem has already been identified by the previous comments - you cannot alter a string literal.
Also, the line of code
i--;
has to be placed as well, with the reason well clarified above.
While making an editable version of the string may be a good way of fixing the problem, a more straightforward way would be to make it a local string, as such :
char b[] = "oooat";
but doing this will make it incompatible with the return type of your my_unique function (char*). But why would you need a return type in the first place, if you are fixing the string itself?
My final code would look like this :
void my_unique(char *first, char *last) {
char* ret=first;
for(int i=0; first+i!=last; i++){
if(first[i]==first[i+1]){
for(int j=i; first+j!=last; j++)
first[j]=first[j+1];
last--;
i--;
}
}
}
making the function return void.
Hope this helps.

Capitalizing all copies of a word: why does this code fail when it's in its own function?

I have two C-style strings:
char st[100] = "to be or not to be ";
char sub_s[100] = "be";
I need to find the beginning of the "be" with strstr(st, sub_s) and change it to capital letters. The new string needs to be `"to BE or not to BE ";
I manage to do it with out the function like so:
void main()
{
char st[100] = "to be or not to be ";
char sub_s[100] = "be";
char* p;
int i;
while (p = strstr(st, sub_s))
{
for (i = 0; i < strlen(sub_s); i++)
{
p[i] -= 32;
}
}
printf("%s\n", st);
}
But when I put this code into its own function it doesn't work any more:
void main()
{
char st[100] = "to be or not to be ";
char sub_s[100] = "be";
replaceSubstring(st, sub_s);
}
void replaceSubstring(char* str, char* substr)
{
int* p;
int i;
while (p = strstr(str, substr))
{
for (i = 0; i < strlen(substr); i++)
{
p[i] -= 32;
}
}
printf("%s\n", st);
}
What's going on here?
In the function that you've written, you've set the type of p to be an int*, not a char *. This means when you write
p[i] -= 32;
the compiler will assume each element pointed at by p is an int and therefore take a step of size sizeof(int) in memory rather than a step of size 1 in memory. In other words, the code is interpreted as
Start at the location pointed at by p.
Jump forward i * sizeof(int) bytes.
Read an integer value from that location.
Subtract 32 from it.
Write it back
rather than
Start at the location pointed at by p.
Find the character i steps down from there.
Subtract 32 from that character.
To fix this, change the type of p to be char*, not int*.
This is the sort of error that would likely be easily detected if you cranked the compiler warning level up to maximum. I would strongly recommend doing that when you're learning to code, then asking questions about the warnings you get when you don't understand them.
Some other stray notes:
The return type of main should be int, not void.
Rather than subtracting 32 from each character, which works but isn't the clearest thing in the word, consider using the tolower function from the <ctype.h> header.
If the substring you're searching for consists solely of non-letter characters (say, ":-)"), then this code can cause an infinite loop. Do you see why? Think about how you might fix it.

How to return a multidimensional character array from a function in a header file in C

I have a main file, and a header file.
In main file, I want to return a 2D char array from a char function from header file. My char function is as following:
char character_distribution(int length, char redistribution[length][2])
{
char *buffer, distribution[256][2] = {0};
long lSize;
struct Bar result = funct();
buffer = result.x;
lSize = result.y;
length = collect_character_distribution(buffer, lSize, distribution);
reorganize_character_distribution(length, distribution, redistribution);
return redistribution;
}
And my main function is as follows:
#include <stdio.h>
#include "character_distribution.h"
void main()
{
int length;
char distribution[length][2];
distribution = character_distribution(length, distribution[length][2]);
int a;
for(a = 0; a < length; a++)
{
printf("%c\n", distribution[a][0]);
}
}
When I run my code, I get the following error:
warning: return makes integer from pointer without a cast
How can I fix the problem?
void character_distribution(int length, char redistribution[][2])
{
char *buffer, distribution[256][2] = {0};
long lSize;
struct Bar result = funct();
buffer = result.x;
lSize = result.y;
length = collect_character_distribution(buffer, lSize, distribution);
reorganize_character_distribution(length, distribution, redistribution);
}
int main()
{
int length = 2; //initialize
char distribution[length][2];
character_distribution(length, distribution);
int a;
for(a = 0; a < length; a++)
{
printf("%c\n", distribution[a][0]);
}
return 0;
}
If you really have to return the 2d array, one way (easy way) is to just put it in a struct
struct distribution_struct {
char x[256];
char y[2];
};
struct distribution_struct character_distribution(int length, char redistribution[][2]) {
struct distribution_struct dis;
//initialize the struct with values
//return the struct
}
And another way is to manually allocate memory for the 2d array in the function and return it
char** character_distribution(int length, char redistribution[][2]) {
//use malloc to create the array and a for loop to populate it
}
You cannot actually return an array from a C function. You can, however, return a pointer to such an array. The correct declaration in that case is:
char (*character_distribution(int length, char redistribution[][2]))[][2]
Sizing the initial dimension is not necessary and not, I suspect, actually conformant with standard C (at least, sizing it with length as you did in your question looks dubious to me). This is because arrays are passed by reference implicitly (and in this case, returned by reference explicitly) and it is not necessary to know the first dimension in order to calculate the address of an element having been given a pointer to the array (and the indices).
Note that you should not return a pointer to an array that is scoped locally to the function, since its storage is deallocated once the function returns (and such a pointer would then be invalid).
However, your question shows that you don't really need to return an array. Since arrays are passed by reference anyway, altering the passed-in array will causes changes that are also visible to the caller. Your code could be written as:
void character_distribution(int length, char redistribution[][2])
{
char *buffer, distribution[256][2] = {0};
long lSize;
struct Bar result = funct();
buffer = result.x;
lSize = result.y;
length = collect_character_distribution(buffer, lSize, distribution);
reorganize_character_distribution(length, distribution, redistribution);
}
And
#include <stdio.h>
#include "character_distribution.h"
void main()
{
int length = 256; // you need to initialise this...
char distribution[length][2];
// No assignment needed here!:
character_distribution(length, distribution /* [length][2] - remove this! */);
int a;
for(a = 0; a < length; a++)
{
printf("%c\n", distribution[a][0]);
}
}
(Of course this relies on the various other functions you call performing as they are supposed to).
Change the signature to this:
char** character_distribution(int length, char redistribution[length][2])
You are returning a multidimensional array, not a character.

How to declare array size with a parameter in c

Im writing a function in c and here is my code:
char* makeMoves(char oldBoard[], int moveType, int empties, char player){
int oldBoardLength;
oldBoardLength = sizeof(oldBoard) / sizeof(oldBoard[0]);
char result[oldBoardLength];
copyBoard(oldBoard, result);
}
I think that this line has a problem:
char result[oldBoardLength];
how can i create this array with length=oldBoardLength?
In java is something like this:
char[] result = new char[oldBoard.length];
but in c i don;t know how to create this. Can anyone help me?
In C, you have to allocate dynamic storage in such cases.
char *result = malloc(oldBoardLength);
copyBoard(oldBoard, result);
free(result);
However, you have to pass oldBoardLength into the function, because an argument like arr[] or arr[8] will always decay to a pointer. Taking sizeof on a pointer is not what you have intended. Have a look at the output of this example:
#include <stdio.h>
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
long int test(char array[16]) {
return COUNT_OF(array);
}
void main(void) {
char a[16];
printf("%ld\n", COUNT_OF(a)); // prints 16
printf("%ld\n", test(a)); // prints 8 or 4 for 64bit or 32bit systems
}
First I would use char *oldBoard instead of char oldBoard[] There the same but I think char *oldBoard is clearer. Second you don't wan't to use sizeof as that will not return the correct length, you would just get the size of a pointer. sizeof(oldBoard) / sizeof(oldBoard[0]); only works on statically allocated arrays or at least that is what this says How do I find the length/number of items present for an array? . Use a another variable to keep track of the array length. Finally use dynamic allocation aka malloc() so that the values don't become garbage when you pass them between functions. I'm not quite sure what you are trying to do but here is a example of what I think your trying to do.
char *makeMoves(char *oldBoard, int len, int moveType, int empties, char player)
{
char *result;
result = malloc(len);
if(result == NULL)
{
return NULL;
}
copyBoard(oldBoard, result);
return result;
}
int main(void)
{
char *board, *result;
int len = 10;
int moveType, empties;
char player;
board = malloc(len);
if(board == NULL)
{
return -1;
}
result = makeMoves(board, len, moveType, empties, player);
if(result == NULL)
{
return -1;
}
free(board);
free(result);
return 0;
}
In C, the most often used idiom is passing the expected number of elements your pointer parameter points to as a separate parameter. Should be something like this:
char* makeMoves(char *oldBoard, int oldBoardLength, int moveType, int empties, char player) {
/* ... */
}
This way, the caller of your function is repsonsible for passing in the correct length.

in C what is the proper way to pass a pointer to a string from a function to the main

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;
}

Resources