0

Like I declared an array of pointer in a loop and use to it collect a bunch of strings by scanning, but after the loop when I want to print out the strings in the array, I found that all arrays are occupied by the same string, which is the latest one I input. The code is as following

#include<stdio.h>

int main(){

char *array[3];
char content[10];
int time;
scanf("%d",&time);

for(int i=0;i<time;i++){
  scanf("%s",content);
  array[i]=content;


}

for(int j =0;j<time;j++){
  printf("%s\n",array[j]);
}

return 0;}
2
  • You are overwriting content.. you only store a pointer to it, not a deep copy Commented Jan 28, 2020 at 4:09
  • 1
    How may addresses does content have? After you assign content to every pointer, what address do you expect each pointer to point to? Commented Jan 28, 2020 at 4:22

2 Answers 2

1

On every iteration input stream is copied to content and address of content is stored in array. Hence content is overwritten again and again and address is copied to every index of array

for(int i=0;i<time;i++){
  scanf("%s",content);
  array[i]=content; //Here every array[0..time] is assigned with address of content
}

Which in short behaving as follows,

enter image description here

what you need is new storage location for every iteration which either dynamically allocated through malloc from heap as follows,

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    int i;
    char content[10];
    int n;
    scanf("%d", &n);
    char*array[n];
    for (i = 0; i < n; i++) {
        scanf("%9s", content); //ensure no more than 10 letter added includeing '/0'
        char*c = malloc(strlen(content)); 
        if(c)
        {
            strncpy(c,content,strlen(content));
            array[i] = c;
        }
        else
        {
            exit(0);
        }
    }

    for (i = 0; i < n; i++) {
        printf("%s\n", array[i]);
    }

    for (i = 0; i < n; i++) {
        free(array[i]);
    }

}

or through VLA's as follows,

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    int i;
    int n;
    scanf("%d", &n);
    char*array[n];
    char content[n][10];
    for (i = 0; i < n; i++) {
        scanf("%9s", content[i]);
        array[i] = content[i];
    }

    for (i = 0; i < n; i++) {
        printf("%s\n", array[i]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have an array of pointers. You still need to allocate memory for each array.

for(int i=0;i<time;i++){
  ret = scanf("%s",content);
  if (ret == 0) { exit(1); // handle error}
  array[i] = malloc(strlen(content)+1);
  if (array[i] == NULL) {  exit(1); // handle error}
  strcpy(array[i],content);
}

Remember to free at the end

for (int i=0; i<time; i++)
{
  free(array[i]);
}

2 Comments

nit... "You still need to allocate memory for each string and assign the beginning address to the next available pointer..." (also point out that you cannot use any input function correctly without checking the return)
And even malloc can fail and indicate via return value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.