Problems with array of strings

This is a discussion on Problems with array of strings within the C Programming forums, part of the General Programming Boards category; Hello, I would like to as how shoul i input strings into array from keyboard? Thanks in advance. my code ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    21

    Question Problems with array of strings

    Hello,
    I would like to as how shoul i input strings into array from keyboard?

    Thanks in advance.

    my code is:

    Code:
    #include <stdio.h>
    
    void main () {
    
     char *string[3];
     int i;
    
     for(i=0;i<3;i++)
         scanf("%s",&string[i]);
    
     for(i=0;i<3;i++)
          printf("%s",string[i]);
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Firstly, don't use void main, use int main.
    Secondly, you must have some sort of space to store the strings. Simply defining a pointer will not give you space. You have to manually call malloc and later free to do that. Or you can simple use an array.

    char string[3][100];

    Or the like.
    This is very important. Be very careful when using scanf to read strings. Read this for more information:
    Need help with altering info on a txt file
    Last edited by Elysia; 03-16-2008 at 11:42 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    probably char string[3][100] to have 3 strings of 100 bytes each
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Right. Good correction. Not used to 2D arrays, since I hardly need them in C++...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    ok, i tired to correct mistakes.. but..there are some left..

    Code:
    #include <stdio.h>
    
    int main () {
    
    	char string[3][100];
    	int i, j;
    
            j=0;
    		while (j<4)
    		{fgets(*string,100,stdin);
    	    for(i=0;i<100;i++)
    			{if(string[j][i]=='\n')
    				{string[j][i]='\0';
    			     j+=1; break;}}
            return 0;
    Last edited by CactusC; 03-16-2008 at 12:01 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Erm. This will fail to compile in every sort of way.
    You need to learn indenting. You can use an auto-indenting IDE such as Visual Studio or Code::Blocks if on Windows.
    And as for the fgets... do you know how arrays work?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. indentation
    2. you should update different strings
    3. use for loop
    Code:
    for(j=0;j<3;j++)
    {
       fgets(string[j], sizeof string[j], stdin);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Quote Originally Posted by Elysia View Post
    Erm.
    And as for the fgets... do you know how arrays work?
    no..i'm new to C.. that's why i'm having problems with such a simple task

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Thanks very much, i did it (atctually you did it) I had some practice programing with Delphi, but it's much easier to work with strings there

    And the other thing i wanted to ask what should i do, if i need other chars, which are not stored in ANSI C, for example Russian.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    If you want to attempt non-english languages, I suggest you use unicode or wide char.
    Usually denoted by wchar_t instead of char.
    Wide strings also uses an L before the string, so "My string" would be come L"My string".
    You also need to use appropriate library functions. But they should all be available in documentation if you need to find them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    But if I need to save all Russian chars, isn't it possible to set whole language alphabet somehow, using UTF-8 locale?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    I imagine it might be, but code locales isn't directly my area.
    If you use wide chars, you don't really have much about code locales to worry about. Or, at the very least, less to worry about.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    So if I'll try to solve this problem using wide chars, I should define every char, which isn't included to ANSI C?

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by CactusC View Post
    But if I need to save all Russian chars, isn't it possible to set whole language alphabet somehow, using UTF-8 locale?
    You do have russian encoding in ANSI - like Windows(Cyrillic), KOI-8 or ISO(Cyrillic) or DOS(Cyrillic)

    You will have encoded russian chars in the codes from 128 to 255 (using different tables for each of the above) - and be able still work with the standard string functions

    If you go for the wide char encoding - you will need to change the type od the string to wchar
    and the function used to the w-version
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Worksafe Array of Strings
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 03-29-2008, 12:00 AM
  2. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 10:35 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  5. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21