Question about strings in ansi C


 
Thread Tools Search this Thread
Top Forums Programming Question about strings in ansi C
# 1  
Old 01-20-2010
Question Question about strings in ansi C

How do I make an array of strings in C? I know that a string is an array of chars, so what I need is an array of char arrays. Basically what I would like to have is the following:

Code:
filenames[0] = "file_a.dat";
filenames[1] = "file_b.dat";

Is this even possible?
# 2  
Old 01-20-2010
Think about it. A "string" is an array of characters. If you want to have an array of strings, substitute the word "string" with what it really is, and you'll get "array of arrays", or an multi-dimensional array. Example:
Code:
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    char files1[2][20];
    strncpy(files1[0], "file1.txt", 10);
    strncpy(files1[1], "file2.txt", 10);

    printf("%s\n", files1[0]);
    printf("%s\n", files1[1]);

    char *files2[] = { "file3.txt", "file4.txt" };

    printf("%s\n", files2[0]);
    printf("%s\n", files2[1]);
}

# 3  
Old 01-20-2010
In addition to pludi's answer. if you're working with constant strings, you can declare the array as follows:
Code:
char* mystrings[] = 
{
"hello, world",
"this is the 2nd string",
"and another one, unbelievable isn't?"
};

Cheers,
Loïc.

Last edited by Loic Domaigne; 01-20-2010 at 04:40 PM.. Reason: typo
# 4  
Old 01-29-2010
my answer

we can solve this one by taking array of pointers.
eg:

Code:
#include<iostream.h>
#include<conio.h>
main()
{
      char*a[2];
      a[0]="aaaaaaa";
      a[1]="bbbbbbb;
      cout<<a[0]<<endl<<a[1];
      getch();
      return 0;
}

output is
aaaaaaa
bbbbbbb

Last edited by pludi; 01-29-2010 at 01:08 PM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C fdopen with and without -ansi

I have very little experience with gcc compilation under different environments, so please bear with me. I carried over 20 years old project into Ubuntu 18.04, it has old style K&R parameters, no function declarations to speak of, many functions without return are not declared void, and on and... (8 Replies)
Discussion started by: migurus
8 Replies

2. Shell Programming and Scripting

Question about Strings in Python

Hi, I get he values for nmval=MS1 & csval=Cluster from the properties file like below. nmval=configProps.get("SVR_NAME") csval=configProps.get("CLS_NAME")What should i do in the commands below so as to use the variables nmval and csval instead of manually typing MS1 and Cluster I want to... (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

4. Programming

why the implementatoin of Bakery algorithm in ANSI C does not work in ANSI C

I follow the description of wiki (Lamport's bakery algorithm - Wikipedia, the free encyclopedia), then implement that algorithm in C, but it doesn't work, Starving is still here, is the implementation worry? Only print out: Thread ID: 0 START! Thread ID: 0 END! Thread ID: 0 START!... (2 Replies)
Discussion started by: sehang
2 Replies

5. HP-UX

Unix_ANSI to PC-ANSI

I want to convert a file from Unix-ANSI to PC-ANSI format. How can i achieve that? (0 Replies)
Discussion started by: ssmallya
0 Replies

6. Shell Programming and Scripting

Convert file from Unix - ANSI to PC - ANSI

Hi, I am creating a file in Unix using a shell script. The file is getting created in the Unix - ANSI format. My requirement is to convert it to the PC - ANSI format. Can anyone tell me how to do this? Thanks, Sunil (0 Replies)
Discussion started by: ssmallya
0 Replies

7. Shell Programming and Scripting

Another 10 second question. Assigning 2 strings to one string.

Hi, I am trying to combine 2 strings into one new string. I know there are existing threads on this topic, but I am having troubles. The variables have variables within their names which is causing me problems. Bad subsitution is the error. The problem line is in red below. thanks (again) to... (1 Reply)
Discussion started by: rorey_breaker
1 Replies

8. Programming

hint on ansi c

I am a student. And need help on following program. I want to make a c program. I have to scan a sentence and I have to interchange a word from that sentence. Example: Scan the sentence is " Drilling machine and Milling machine " . Replace the word "machine" by "operation". And output should... (2 Replies)
Discussion started by: dhaval chevli
2 Replies

9. HP-UX

ANSI / C Compiler for HP-UX 11.11

Good Day I downloaded Server Evaluation copy of C/ANSI compiler, but when I try to compile a file with it, it gives me following error - (for HP-UX 11.11 v1 PA-RISC) Internal Error: Codeword file /opt/ansic/newconfig/ansic.cwd missing or empty. Detailed Errors are as follows Internal... (3 Replies)
Discussion started by: shawnbishop
3 Replies

10. Programming

Ansi C

Dear All, I have to develope some C functions in Unix for a Magic program. The original MSE code which compiles the attached C program uses a +z option, but the cc compiler don't know this. The complete command in the compiler script is 'cc -c -Aa +z myfile.c'. The warning message is 'The -z... (4 Replies)
Discussion started by: Frankie
4 Replies
Login or Register to Ask a Question