-2
int main()
{
    int i, Quant, *Qsize1[4];
    char *size1[4], size[5];
    for(i=0; i<3; i++)
    {
        printf("Select Size : (S, M, L, XL) ");
        scanf("%s",size);
        size1[i]=size;

        printf("How much quantity for this size? : ");
        scanf("%d",Quant);
        Qsize1[i]=Quant;
    }
    for(i=0; i<3; i++)
    {
        printf("\nSize %s : %d",size1[i], Qsize1[i]);
    }
    return 0;
}

example of print that i want is = Size L : 25 but when i print the it will print the last size i enter. also the quantity of that size is wrong.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Ismat
  • 1
  • 2

1 Answers1

0
  1. All the sizes are the same when you print them because you're reading all of them to the same character array named size. Each iteration overrides the previous size, and then you end up with size1[0], size1[1], size1[2] all point to the last size.

  2. Quantity is wrong because you should pass scanf the address of the integer: scanf("%d", &Quant);

Also, as mentioned in the comments, you should fix how you read size itself. See this question about how to do it properly.

SHG
  • 2,516
  • 1
  • 14
  • 20
  • I want to store the current value size that read from user to size1[i]. So I assume first size will be store in size[0], second will be store in size1[1], so i assume it will store in different array – Ismat May 23 '17 at 02:21
  • `size1` is an array of **pointers**. So you store in its indices **pointers** or **addresses**. In the 1st iteration, you store in `size1[0]` the **address** of `size`. In the 2nd iteration, you store in `size1[1]` the **address** of `size`. In the 3rd iteration, you store in `size1[2]` the **address** of `size`. Therefore, all the elements of the array `size1` are similar, same address. An address can only have one value stored in, and it will be the *last* value stored, in the last iteration. Understand now? – SHG May 23 '17 at 02:33
  • i got right print for Qsize[i], but not for size1[i], the difference between them is Qsize[i] is integer, while size[i] is string. – Ismat May 23 '17 at 03:07
  • You can fix it few ways. I'd change the type of `size1` to `char size1[4][5]` (the `5` is because that's the size of `size`) and then instead of `size1[i]=size;` I would go `strncpy(size1[i], size, sizeof(size));`. This way you *copy* (unlike pointer which just points) the content of `size` into `size1[i]`. – SHG May 23 '17 at 03:17
  • really thanks lot to you. I finally got it. It really a big help for me since this is for my final project – Ismat May 23 '17 at 03:33