配列に文字列を挿入できるように、特定のインデックスのすべての要素を動的に割り当てられたchar **の右側に移動する必要があります。cで動的に割り当てられたchar **ポインタの右に要素を移動する方法は?正しい軌道にいるのですか?
特定のインデックスに格納されている文字列を横断して右に移動する方法が混乱していますか?
この関数は、インデックスと、構造体SmartArrayへのポインタと、そのインデックスに挿入されるchar * str文字列を受け取ります。
私は適切なトラックにいますか?これにはより効率的な方法がありますか?
これは私がこれまでに作ってみたものです:
char *insertElement(SmartArray *smarty, int index, char *str)
{
int i;
char temp;
// Any elements to the right of index are shifted one space to the right., not sure if this is correct way to find strlen
for (i = index; i < strlen(smarty->array[index]); i++)
{
temp = smarty->array[index]
if (i == index)
{
smarty->array[index] = str[i];
}
else
{
smarty->array[index] = temp;
}
}
}
これは、私が働いている構造体である:
typedef struct SmartArray
{
// We will store an array of strings (i.e., an array of char arrays)
char **array;
// Size of array (i.e., number of elements that have been added to the array)
int size;
// Length of the array (i.e., the array's current maximum capacity)
int capacity;
} SmartArray;
これは、['memmove'](http://en.cppreference.com/w/c/string/byte/memmove)の良い使用例です。 –
[mcve]を入力してください。すべての型がどのように定義されているか、メモリがどのように割り当てられたかなどを正確に把握する必要があります。 – kaylum
ああ、あなたが表示するコードには論理的な誤りがあります。 –