Dynamic array of pointers - arrays

I have an array of pointers to strings.
char **array;
I declare it this way and not char *array[N] because this array won't have a static number of elements.
Declaring the array this way, I will probably have to realloc the sizeof it every time I add a new element (pointer to string).
int main(void)
{
char **array;
char *word = "lolol";
char *word2 = "blabla";
return 0;
}
Could you give me an example on how I should "create space" in the array in order to store pointers to these strings?

The best way of doing it is probably by making a struct
This way, you can resize it, and add as many strings as you want without needing to choose a specific size.
Note: setting the string_array's capacity and size to 0 is necessary for it to work.
You could do it by a function like this instead:
void load_array(string_array *array)
{
array->size = 0;
array->capacity = 0;
}
And call it like this:
load_array(&my_array);
Note, when getting the value from one of these arrays using [], you must call it like this:
my_array.arr[index]
This is because you must refer to the pointer in the array struct, which is as arr (char **)
I have tested the below, and it works perfectly.
# include <stdio.h>
typedef struct string_array
{
char **arr;
unsigned capacity, size;
} string_array;
void add_to_array(string_array *array, char *str)
{
if(array->capacity == 0)
{
array->arr = (char **)malloc((array->capacity = 3) * sizeof(char *));
array->arr[array->size++] = str;
}
else if(array->capacity == array->size)
{
array->arr = (char **)realloc(array->arr, (array->capacity *= 1.5) * sizeof(char *));
array->arr[array->size++] = str;
}
else
{
array->arr[array->size++] = str;
}
}
int main(void)
{
char *str1 = "Hello World";
char *str2 = "Hello World2";
char *str3 = "Hello World3";
char *str4 = "Hello World4";
char *str5 = "Hello World5";
string_array my_array;
my_array.capacity = 0;
my_array.size = 0;
add_to_array(&my_array, str1);
add_to_array(&my_array, str2);
add_to_array(&my_array, str3);
add_to_array(&my_array, str4);
add_to_array(&my_array, str5);
// and so on
for (int i = 0; i < my_array.size; ++i)
{
printf(my_array.arr[i]);
printf("\n");
}
free(my_array.arr);
getchar(); // this means pressing enter closes the console
return (0);
}

Here is a demonstrative program
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
size_t N = 2;
char **array = malloc( N * sizeof( char * ) );
if ( !array ) return 1;
char *word = "lolol";
char *word2 = "blabla";
array[0] = word;
array[1] = word2;
char *word3 = "Hello";
++N;
array = realloc( array, N * sizeof( char * ) );
if ( !array ) return 2;
array[2] = word3;
for ( size_t i = 0; i < N; i++ ) puts( array[i] );
free( array );
return 0;
}
The program output is
lolol
blabla
Hello

Simply allocate some room for pointers in your array, and free it when you're done.
char *word = "lolol";
char *word2 = "blabla";
char** array = malloc(2*sizeof(char*));
array[0] = word;
array[1] = word2;
free(array);
You can change that 2*sizeof(char*) to a N*sizeof(char*) if you want more elements.

Related

How to reverse a string with pointers only?

I'm trying to reverse a string, but it just stays the same. I don't use any modules except <string.h> and <stdio.h>.
void rev(s){
char i, temp;
char *sf = s;
char ri = strlen((s) - 1);
char *sl = &s[ri];
for (i = 0; i < ri; i++){
if (*sf != *sl){
temp = *sf++;
s[i] = *sl--; //
s[ri--] = temp; //those two seems to be getting new characters, but it won't
}
else {
ri--;
sf++;
sl--;
}
}
printf("%s", s);
}
The function will not compile at least because the parameter does not have a type specifier.
void rev(s){
The type char has a little range of acceptable values. So you shall not use it for calculating the length of a string.
The call of strlen in this declaration
char ri = strlen((s) - 1);
invokes undefined behavior. It seems you mean
char ri = strlen(s) - 1;
that also can invoke undefined behavior for an empty string.
This loop
for (i = 0; i < ri; i++){
does not use pointers.
The function can be defined the following way as it is shown in the demonsytrative program below.
#include <stdio.h>
#include <string.h>
char * reverse( char *s )
{
if ( *s )
{
for ( char *first = s, *last = s + strlen( s ); first < --last; ++first )
{
char c = *first;
*first = *last;
*last = c;
}
}
return s;
}
int main( void )
{
char s1[] = "1";
char s2[] = "12";
char s3[] = "123";
puts( reverse( s1 ) );
puts( reverse( s2 ) );
puts( reverse( s3 ) );
}
The program output is
1
21
321
A simple solution:
char *sl = sf;
while (*sl != 0)
++ sl;
-- sl;
while (sf < sl)
{
char c = *sf;
*sf = *sl;
*sl = c;
++sf, --sl;
}
Find the end of the string by skipping all characters until you find the NUL (zero) character.
Then step back one character (decrement sl) so that you have pointers to the first and the last character of the string.
Then walk both pointers towards one another and swap characters until the pointers meet or cross.
Your code has plenty of issues (bugs 🐛):
char ri = strlen((s) - 1); has to be size_t ri = strlen((s)) - 1;
Other code is very hard to analyze as you use not self explanatory variable names.
Here you have a bit more simple code and much easier to analyse.
char *reverseString(char *str)
{
char *wrk = str, *end;
if(str && *str)
{
end = str + strlen(str) - 1;
while(end > wrk)
{
char temp = *wrk;
*wrk++ = *end;
*end-- = temp;
}
}
return str;
}
int main(void)
{
char str[] = "1234567890";
printf("reversed: %s\n", reverseString(str));
}

Qsort doesn't sort array of pointers to strings

In this C program I read in words typed with my keyboard to a char pointer. The pointer is stored in an pointer array. Then I want to sort the array with qsort by the function comparison. I give it the pointer to my pointer array.
It doesnt sort the array at all. I dont know if I got here a UB, or I miss memory by wrong allocation.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
bool read_word(char ***a, int *length);
int comparison(const void *p, const void *q);
int main()
{
int *length = malloc(sizeof(int));
*length = 0;
char **array = malloc(sizeof(char *));
bool go = false;
while(go == false)
{
printf("Enter word: ");
go = read_word(&array,length);
}
qsort(array, *length - 1,sizeof(char *), comparison);
printf("\n");
for(int i = 0; i < *length; i++)
printf("%s\n", array[i]);
return 0;
}
bool read_word(char ***a, int *length)
{
char ch;
++*length;
char *word = malloc(20 * sizeof(char) + 1);
char *keep_word;
char **temp = realloc(*a,*length * sizeof(*a));
if(!temp)
exit(EXIT_FAILURE);
*a = temp;
keep_word = word;
while((ch = getchar()) != '\n')
*keep_word++ = ch;
*keep_word = '\0';
if(word == keep_word)
{
free(word);
--*length;
return true;
}
(*a)[*length - 1] = word;
printf("%s", (*a)[*length -1]);
printf("\nh\n");
return false;
}
int comparison(const void *p, const void *q)
{
const char *p1 = p;
const char *q1 = q;
return strcmp(p1,q1);
}
change to
int length = 0;//no need malloc
char **array = NULL;//first value is NULL
qsort(array, length, sizeof(char *), comparison);//not length-1
int ch;//type of return value of getchar is int
for(int i = 0; i < 20 && (ch = getchar()) != '\n'; ++i)//i : guard
const char *p1 = *(const char **)p;//qsort pass pointer to object to comparison function
const char *q1 = *(const char **)q;//if char *, char **
You do
keep_word = word;
and later
if(word == keep_word)
The condition in brackets is always true.
On a side note, your program is error-prone and hard to understand because you use pointers way too much. In main(), length should be int, not int*, array should be char*, not char** . In the read_word, a should be char**, not char***. Don't use pointers unless necessary!

Reversing a C string function crashes?

I'm trying to write a C function to reverse a passed in C style string (ie char *) and return the char pointer of the reversed string. But when I run this in VS2012, nothing is printed in terminal and "main.exe has stopped working" msg shows up.
#include <stdio.h>
#include <string.h>
char * rrev_str(char * str )
{
char *revd_str=""; //I tried char revd_str []="" error: stack around "revd_str" is corrupted
int i,r;
int str_len=strlen(str);
for (i = str_len-1, r=0; i >=0; i--,r++)
{
revd_str[r]= str[i];
}
return revd_str;
}
int main(int argc, char* argv[])
{
char str1 [] ="STEETS";
char str2 [] ="smile everyday!";
//reverse "chars" in a C string and return it
char * rev_string=rrev_str(str1);
}
The problem here is three fold. First you aren't allocating enough space for the reversed string, and secondly you are returning a pointer to a local variable in rrev_str(), and thirdly you're modifying a string literal. You need to allocate space for revd_str on the heap:
char * rrev_str(char * str )
{
int i,r;
int str_len=strlen(str);
char *revd_str=malloc(str_len + 1);
memset(revd_str, 0, str_len + 1);
for (i = str_len-1, r=0; i >=0; i--,r++)
{
revd_str[r]= str[i];
}
return revd_str;
}
Problem: You are accessing invalid memory address.
revd_str is pointing to literal constant string of length 1 and you are accessing it beyond the length which is invalid.
Solution:
Create char array of require length (statically or dynamically).
Reverse the given string.
Pass 2nd param as destination string
syntax: char * rrev_str(char * src, char *dest);
Reverse the given string
char * rrev_str(char * str )
{
int start = 0;
int end = strlen(str) - 1;
char temp;
for (; start < end; start++ ,end--)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
}
return str;
}
int main(int argc, char* argv[])
{
char string [] ="smile";
//reverse "chars" in a C string and return it
char * rev_string = rrev_str(string);
printf("%s",rev_string);
}
Pass 2nd param as destination string
char * rrev_str(char * src, char *dest)
{
int srcLength = strlen(src);
int destLength = strlen(dest);
int i;
// Invalid destination string
if (srcLength > destLength)
{
return NULL;
}
dest[srcLength] = '\0';
srcLength--;
for (i=0; srcLength >= 0;i++, srcLength--)
{
dest[i] = src[srcLength];
}
return dest;
}
int main(int argc, char* argv[])
{
char string [] ="smile";
char revString[20];
//reverse "chars" in a C string and return it
char * rev_string = rrev_str(string, revString);
printf("%s",rev_string);
}
What! you are doing..
char *revd_str=""; // Creating String Literal which can't be modified because they are read only
char *revd_str[]=""; // Creating Char Array of Size Zero.
So Solution are
Either take reference of your string
char *revd_str = strdup(str);
Or create dynamic char array
char *revd_str = (char*) malloc (strlen(str)+1);
your program will run fine. logic is incorrect for reversing so modify it. A sample solution is given below
char * rrev_str(char * str )
{
char *revd_str=strdup(str);
int i; // no need for extra 'int r'
int str_len=strlen(str);
for (i = 0; i < str_len/2; i++)
{
char temp = revd_str[i];
revd_str[i]= revd_str[str_len - 1 -i];
revd_str[str_len - 1 -i] = temp;
}
return revd_str;
}

How to malloc inside a function and return pointer in C?

Below is some psudo, but I'm trying to accomplish this. The problem is as written, it returns a blank pointer.
int testFunction(char *t) {
int size = 100;
t = malloc(100 + 1);
t = <do a bunch of stuff to assign a value>;
return size;
}
int runIt() {
char *str = 0;
int str_size = 0;
str_size = testFunction(str);
<at this point, str is blank and unmodified, what's wrong?>
free(str);
return 0;
}
This works fine if I have a predefined size, such as char str[100] = "" and I don't try to malloc or free memory afterwords. I need to be able to make the size dynamic though.
I've also tried this, but seem to run into a corrupt pointer somehow.
int testFunction(char **t) {
int size = 100;
t = malloc(100 + 1);
t = <do a bunch of stuff to assign a value>;
return size;
}
int runIt() {
char *str = 0;
int str_size = 0;
str_size = testFunction(&str);
<at this point, str is blank and unmodified, what's wrong?>
free(str);
return 0;
}
Thanks!
Your test function is just a bit backward. Size should be an input. The allocated pointer should be the output:
char* testFunction(int size) {
char* p = malloc(size);
<do a bunch of stuff to assign a value>;
return p;
}
int runIt() {
char *str = 0;
int str_size = 100;
str = testFunction(str_size);
<do something>
free(str);
return 0;
}
edit
Per comment, making size an output too.
char* testFunction(int *size) {
*size = <compute size>;
char* p = malloc(size);
<do a bunch of stuff to assign a value>;
return p;
}
int runIt() {
char *str = 0;
int str_size;
str = testFunction(&str_size);
<do something>
free(str);
return 0;
}
You're nearly there with the second example, but change
int testFunction(char **t) {
...
t = malloc(100 + 1);
To
int testFunction(char **t) {
...
*t = malloc(100 + 1);
The point being that you're passing in a char**, a pointer to a pointer, so you want to assign the malloc to what that points at (a pointer).
I am also studying c++. I had a the same question. So after speaking to c++ pro at work, he suggest me to do something like this
int method(char* p) {
if (p) {
strcpy(p, "I like c++");
}
return strlen("I like c++");
}
int main()
{
char* par = NULL;
int len = method(par);
if (len > 0) {
par = (char*)malloc(len+1);
memset(par, 0, len + 1);
method(par);
cout << "ret : " << par;
}
free(par);
}

Trouble filling a dynamic c array

I am having issues with filling an array of strings that was created dynamically. The array basically contains two strings and I would like to print them both with their lengths. I am getting a very weird result whereby the memory locations are getting mixed up. Please see the code below.
Any suggestions would greatly be appreciated. Thanks.
void printWords(char **words, int numberOfWords)
{
int index;
for (index = 0; index < numberOfWords; index++)
{
printf("%s, %d\n", &(*words)[index], (int)strlen(&(*words)[index]));
}
}
void fillWords(char **words)
{
*words = malloc(2 * sizeof(char *));
char hello[] = {"Hello"};
(*words)[0] = (char)malloc(strlen(hello) * sizeof(char));
strcpy(&(*words)[0], hello); //Copy word into array
char world[] = {"Worldz"};
(*words)[1] = (char)malloc(strlen(world) * sizeof(char));
strcpy(&(*words)[1], world); //Copy word into array
}
int main (int argc, const char * argv[])
{
char *words;
fillWords(&words);
printWords(&words, 2);
return 0;
}
The expected output should be
Hello, 5
Worldz, 6
however I am getting
HWorldz, 7
Worldz, 6
I think you are getting confused between char * and char **.
Also, be sure to allocate enough memory for the null-termination character at the end of the string.
Here is my solution:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void printWords(char **words, int numberOfWords)
{
int index;
for (index = 0; index < numberOfWords; index++)
{
printf("%s, %d\n", words[index], (int)strlen(words[index]));
}
}
char ** createWords()
{
char ** words;
// Allocate memory for an array of pointers, length 2.
words = malloc(2 * sizeof(char *));
char hello[] = {"Hello"};
words[0] = malloc((strlen(hello)+1) * sizeof(char));
strcpy(words[0], hello); //Copy word into array
char world[] = {"Worldz"};
words[1] = malloc((strlen(world)+1) * sizeof(char));
strcpy(words[1], world); //Copy word into array
return words;
}
int main (int argc, const char * argv[])
{
char **words;
words = createWords();
printWords(words, 2);
return 0;
}
I renamed fillWords to createWords and made it return a pointer instead of taking a pointer as an argument. If you really want fillWords to take a pointer as an argument, you can do it, but the argument will have to be of type char ***.
First problem: you can't put 2 words in a char * (well ... you can, but not the way you're doing; and the way to do it is nothing like your code).
You need an "array of strings", or, more C-like, an array of char *:
char *words[2]; /* words[0] and words[1] are pointers to char */
char *wrong; /* wrong[0] is a char; wrong[1] is a char */
So, change the definition of words in your main and check/edit all the other functions for correctness.
int main (int argc, const char * argv[])
{
char *words[2];
fillWords(words);
printWords(words, 2);
return 0;
}
Here is the solution. I was getting confused between char* and char. What I want was an array of chars.
void printWords(char **words, int numberOfWords)
{
int index;
for (index = 0; index < numberOfWords; index++)
{
printf("%s, %d\n", words[index], (int)strlen(words[index]));
}
}
void fillWords(char **words)
{
*words = malloc(2 * sizeof(char *));
char hello[] = {"Hello"};
words[0] = malloc(strlen(hello) * sizeof(char));
strcpy(words[0], hello); //Copy word into array
char world[] = {"Worldz"};
words[1] = malloc(strlen(world) * sizeof(char));
strcpy(words[1], world); //Copy word into array
}
int main (int argc, const char * argv[])
{
char *words;
fillWords(&words);
printWords(&words, 2);
return 0;
}

Resources