Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Read_str(char a[][10],int b);
void Print_str(char a[][10],int b);

int main(void) {
    int b1;
    printf("insert number of string: ");
    fflush(stdout);
    scanf("%d",&b1);
    char a1[b1][10];
    Read_str(a1,b1);
    Print_str(a1,b1);
    return EXIT_SUCCESS;
}

void Read_str(char a[][10],int b){
    int i;
    for(i=0;i<b;i++)
    {
        printf("insert string%d",i+1);
        fflush(stdout);
        scanf("%s",&a[i][10]);
    }
}

void Print_str(char a[][10],int b){
    int i;
    for(i=0;i<b;i++)
    {
        printf("string number%d= %s\n",i+1,a[i]);
        fflush(stdout);
    }
}


this all of cod that i wrote.get some string and print them.
please check this code.when i write this
C++
scanfscanf("%s",&a1[i-1][10]);
the code work good.but when i write this
C++
scanf("%s",&a1[i][10]);
the code did not work good.for example for b1==3 the code did not work .
Posted
Updated 8-Dec-12 4:54am
v6

1 solution

First of all, your indexing is wrong as array starts at index 0 in C. Thus i-1 should really be i.

Then &a[i-1] is not the same address as &a[i-1][10] but the same address as &a[i-1][0].

By the way any expression with i-1 is not valid when i is 0.
 
Share this answer
 
Comments
ho_khalaf 8-Dec-12 9:38am    
i update the code i wrote it wring before.when i use this code and want to sort all string did not work good.when i use i-1 it work good.
Philippe Mori 8-Dec-12 11:34am    
You have to understand that in practice since "strings" are consecutive in memory, if you add 10 to the last indice, the effect is similar to adding 1 to the first indice as each string is 10 characters long.

So to be conceptually correct, you want to point at the beginning of the string (index 0) and not the beginning of the next string (index 10).

As an analogy, if something cost 3$, you won't say it cost 2$ and 100¢...
ho_khalaf 8-Dec-12 13:14pm    
thank you very much.
good luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900