0

I have a string declared as such:

char *mode_s = (char *)calloc(MODE_S_LEN, sizeof(char));

How can I add a char to the end of the array?

M-R
  • 411
  • 2
  • 6
  • 15
  • "available position": what does it mean? –  Nov 14 '16 at 22:26
  • @Arkadiy, I mean concatenate – M-R Nov 14 '16 at 22:29
  • 1
    `sizeof(char)` is defined to be 1, so you have a memory chunk `MODE_S_LEN` bytes long. These can be accessed from `mode_s` at indices 0 through `MODE_S_LEN-1`, so `mode_s[MODE_S_LEN-1]` is the last char in your array. Don't forget to make sure `calloc` gave you a valid memory chunk rather than NULL – yano Nov 14 '16 at 22:31
  • 1
    First, [don't cast the result of `malloc` and friends](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Second, [`sizeof(char)==1`](http://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1-or-at-least-char-bit-8). – n. m. could be an AI Nov 14 '16 at 22:35
  • @n.m. This code was supplied in the assignment and I'm not supposed to modify it. – M-R Nov 14 '16 at 22:37
  • 1
    This is not a "declaration of a string". There is no such thing as a "declaration of a string". – user253751 Nov 14 '16 at 23:04
  • 1) A pointer is not a string. 2) A pointer is not an array. 3) Don't cast the result of `calloc` & friends or `void *` in general. 4) Don't use unnecessary or not understood casts at all. 5) `sizeof(char)` is always `1` by definition. It is useless. – too honest for this site Nov 15 '16 at 08:41
  • Please learn about "C strings" (you can google this exact term). –  Nov 15 '16 at 13:26

3 Answers3

1

Lets assume " first available position " means at index 0.

char *mode_s = (char *)calloc(MODE_S_LEN, sizeof(char));
*mode_s='a';

To store a character at an arbitrary index n

*(mode_s+n)='b';

Use pointer algebra, as demonstrated above, which is equivalent to

mode_s[n]='b'; 

One sees that the first case simply means that n=0.

If you wish to eliminate incrementing the counter, as specified in the comment bellow, you can write a data structure and a supporting function that fits your needs. A simple one would be

typedef struct modeA{
  int size;
  int index;
  char *mode_s;
}modeA;

The supporting function could be

int add(modeA* a, char toAdd){
  if(a->size==a->index) return -1;
  a->mode_s[index]=toAdd;
  a->index++;
  return 0;
}

It returns 0 when the add was successful, and -1 when one runs out of space.

Other functions you might need can be coded in a similar manner. Note that as C is not object oriented, the data structure has to be passed to the function as a parameter.

Finally you code code a function creating an instance

modeA genModeA(int size){
  modeA tmp;
  tmp.mode_s=(char *)calloc(size, sizeof(char));
  tmp.size=size;
  tmp.index=0;
  return tmp;
}

Thus using it with no need to manually increment the counter

modeA tmp=genModeA(MODE_S_LEN);
add(&tmp,'c');
Žiga Sajovic
  • 324
  • 3
  • 5
  • 3
    Wat? Why are you using such bizarre syntax? Use `mode_s[n] = 'b'`. – Qix - MONICA WAS MISTREATED Nov 14 '16 at 22:31
  • Seeing that pointers are giving him trouble, I showed explicit pointer notation. I added the equivalence of notations after your comment. – Žiga Sajovic Nov 14 '16 at 22:33
  • @ŽigaSajovic Thanks, it did occur to me that I could do it your way, however that would require me to increment a counter for every element. Is there not a simpler way to concatenate? – M-R Nov 14 '16 at 22:35
  • @MartinRand I added a simple implementation of a data structure that automates what you want. In C, you usually have to code the data structure you need. I hope this is OK, I don't see any other way of such automation in C (besides coding a different data structure that is). – Žiga Sajovic Nov 14 '16 at 23:12
1

There is no standard function to concatenate a character to a string in C. You can easily define such a function:

#include <string.h>

char *strcatc(char *str, char c) {
    size_t len = strlen(str);
    str[len++] = c;
    str[len] = '\0';
    return str;
}

This function only works if str is allocated or defined with a larger size than its length + 1, ie if there is available space at its end. In your example, mode_s is allocated with a size of MODE_S_LEN, so you can put MODE_S_LEN-1 chars into it:

char *mode_s = calloc(MODE_S_LEN, sizeof(*mode_s));
for (int i = 0; i < MODE_S_LEN - 1; i++) {
    strcatc(mode_s, 'X');
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
-2
char newchar = 'a'; //or getch() from keyboard

//realloc memory:

char *mode_sNew = (char *)calloc(MODE_S_LEN + 1, sizeof(char));

//copy the str:

srncpy(mode_sNew, mode_s, MODE_S_LEN);

//put your char:

mode_sNew[MODE_S_LEN] = newchar;

//free old memory:

free(mode_s);

//reassign to the old string:

mode_s = mode_sNew;

//in a loop you can add as many characters as you want. You also can add more than one character at once, but assign only one in a new position

Wera
  • 357
  • 2
  • 4
  • `srncpy` is not a standard function. `strncpy()` is a standard function, but it does not do what you think nor what you want. **do not use** this function. – chqrlie Nov 14 '16 at 23:15