Click here to Skip to main content
12,253,679 members (54,773 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ 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
scanfscanf("%s",&a1[i-1][10]);
the code work good.but when i write this
scanf("%s",&a1[i][10]);
the code did not work good.for example for b1==3 the code did not work .
Posted 8-Dec-12 2:24am
Edited 8-Dec-12 4:54am
v6

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100