Copying block of chars to another char array in a specific location

For example :

char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";

char letters[3]="MN";

How can I copy "MN" from the second array and replace "mn" in the first array ? if I declare the first array this way :

char array[26] = "abcdefghijklmnopqrstuvwxyz";

Is there any difference between these two initializations?

You need to reserve 27 bytes to store 26 characters plus the required zero terminator. Better to do this, and the compiler will automatically allocate enough space.

char alphabet[] = "abcdefghijklmnopqrstuvwxyz";

You can use memcpy() to transfer bytes or characters from one memory location to another.

Is there any difference between these two initializations?

Two differently named character array variables are created.

  char* pmn = strstr(alphabet, "mn");
  memcpy(pmn, letters, 2);

ToddL1962:

  char* pmn = strstr(alphabet, "mn");

memcpy(pmn, letters, 2);

Better check for NULL first.

-jim lee

jimLee:
Better check for NULL first.

-jim lee

If letters is declared as const then probably no need to.

void * memcpy ( void * destination, const void * source, size_t num );

From this reference on memcpy

The function does not check for any terminating null character in source - it always copies exactly num bytes.

ToddL1962:
If letters is declared as const then probably no need to.

I think jimLee was referring to the return value of strstr(). If you run that code a second time, for example, pmn will be null because alphabet will not contain the string "mn" anymore.

christop:
I think jimLee was referring to the return value of strstr(). If you run that code a second time, for example, pmn will be null because alphabet will not contain the string "mn" anymore.

@jimLee has a good point. My bad.

jremington:
You need to reserve 27 bytes to store 26 characters plus the required zero terminator. Better to do this, and the compiler will automatically allocate enough space.

char alphabet[] = "abcdefghijklmnopqrstuvwxyz";

I thought that char arrays position started from 0, hence from 0-25 i would have the alphabet letters and 26th would have the zero terminator, isnt that correct then?

You need to declare an array of size 27 bytes minimum, indexed as 0-26, to store 26 letters plus the zero terminator.

christop:
I think jimLee was referring to the return value of strstr(). If you run that code a second time, for example, pmn will be null because alphabet will not contain the string “mn” anymore.

Good point, I was not aware of it (new to the language). So I need to make sure the pointer is pointing to a valid content in the alphabet. I’ll increase a bit the complexity of the example so I can have more ideas and understand the language better… The below code is supposed to build a list of alphabets, the start point is the standard alphabet, as the loop goes on we modify it and save within the list. I used the previous idea as @ToddL1962 suggested, but instead of checking if the return of strstr is null I am making sure that the pointer is pointing to not null by an auxiliary char in a recursive way.

#include <SoftwareSerial.h>



char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char letters[3]="MN";
char auxCHAR[3]= "AB";
char * pmn;
char * alphabetlist[] =
{
  
};

int i =0;
void setup()
{
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);

  for(i=0;i<=2;i++){
    if(i == 0){
      strcpy(auxCHAR,letters);
      pmn = strstr(alphabet, "mn");
      memcpy(pmn, letters, 2);
      alphabetlist[i] = alphabet;
      Serial.println(alphabetlist[i]);
    }if(i == 1){
      
      strcpy(letters,"Mn");
      pmn = strstr(alphabet,auxCHAR);
      strcpy(auxCHAR,letters);
      memcpy(pmn,letters,2);
      alphabetlist[i] = alphabet;
      Serial.println(alphabetlist[i]);
      }
      if(i == 2){
        
        strcpy(letters,"mN");
        pmn = strstr(alphabet,auxCHAR);
        strcpy(auxCHAR,letters);
        memcpy(pmn,letters,2);
        alphabetlist[i] = alphabet;
        Serial.println(alphabetlist[i]);
      }
  }

   Serial.println(alphabetlist[1]);
   
}

Why can’t I acess the list content in an specific index outside the loop?

vdsn:

#include <SoftwareSerial.h>

char alphabet = “abcdefghijklmnopqrstuvwxyz”;
char letters[3]=“MN”;
char auxCHAR[3]= “AB”;
char * pmn;
char * alphabetlist =
{
 
};

int i =0;
void setup()
{
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);

for(i=0;i<=2;i++){
    if(i == 0){
      strcpy(auxCHAR,letters);
      pmn = strstr(alphabet, “mn”);
      memcpy(pmn, letters, 2);
      alphabetlist[i] = alphabet;
      Serial.println(alphabetlist[i]);
    }if(i == 1){
     
      strcpy(letters,“Mn”);
      pmn = strstr(alphabet,auxCHAR);
      strcpy(auxCHAR,letters);
      memcpy(pmn,letters,2);
      alphabetlist[i] = alphabet;
      Serial.println(alphabetlist[i]);
      }
      if(i == 2){
       
        strcpy(letters,“mN”);
        pmn = strstr(alphabet,auxCHAR);
        strcpy(auxCHAR,letters);
        memcpy(pmn,letters,2);
        alphabetlist[i] = alphabet;
        Serial.println(alphabetlist[i]);
      }
  }

Serial.println(alphabetlist[1]);
 
}




Why can't I acess the list content in an specific index outside the loop?

alphabetlist is an array of pointers but you haven’t initialized it to any pointers or explicitly told the compiler how many pointers are in the array. Additionally the line:

      alphabetlist[i] = alphabet;

Sets the element i in alphabetlist to a pointer to alphabet. Eventually you will have all the elements in alphabetlist pointing to alphabet, which is kind of useless.

You need to study arrays more. You have some fundamental misconceptions about how arrays are accessed and manipulated.