Question Details

No question body available.

Tags

c

Answers (1)

May 28, 2025 Score: 2 Rep: 789,234 Quality: Medium Completeness: 60%

The assignment elements[i] = element doesn't make a copy of element, it just sets elements[i] to a pointer to the element array. So after the first loop, all the values in elements are pointers to the same array, and its contents are the result of the last snprintf().

You don't need element, just make elements a 2-dimensional array rather than an array of pointers.

void createElementList(int totalnumberofelements, char* baseelement) { char elements[totalnumberofelements][strlen(baseelement) + 6]; for (int i = 0; i < totalnumberofelements; i++) { snprintf(elements[i], sizeof(elements[i]), "%s %d |", baseelement, i+1); // account for string terminator Serial.printf("inside elements[%d] = %s\n", i, elements[i]); } for (int j = 0; j < totalnumberof_elements; j++) { Serial.printf("elements[%d] = %s\n", j, elements[j]); } }