Access a value in 2D array in C program


 
Thread Tools Search this Thread
Top Forums Programming Access a value in 2D array in C program
# 1  
Old 06-16-2012
Access a value in 2D array in C program

Hi All,

I am new to c programming. I am getting compilation error in the below program. Can somebody help me?

Code:
#include<stdio.h>
#include<string.h>
void main()
{
  int i=j=0;
  char a[2][2]={'f1',4,'f2','4'};
  char count;
  for(i=0;i<2;i++)
  {
    for(j=1;j<=2;j++)
    {
      if(STREQ("f1",a[i][j]))
      {
        strcpy(count,a[i][j+1]);
      }
    }
  }
  printf("Count: %s",count);
}

I am expecting the output should be "Count: 4" but it is giving compilation error..Smilie

Last edited by Scott; 06-16-2012 at 06:33 AM.. Reason: Code tags, please...
# 2  
Old 06-19-2012
Code:
#include<stdio.h>
#include<string.h>

void main()
{
  int i=0, j=0;
  char a[4][3]={"f1","4","f2","4"};
  char count[1][3] = {0};
  
  for (i=0; i<=2; i+=2) {
    if (strcmp ("f1", a[i]) == 0) {
      strcpy(count[0], a[i+1]);
    }
  }
  
  printf ("Count: %s\n", count[0]);
}


Last edited by balajesuri; 06-19-2012 at 11:58 AM..
# 3  
Old 06-20-2012
Quote:
Originally Posted by sam_14189
I am expecting the output should be "Count: 4" but it is giving compilation error..Smilie
Giving what compilation error? They're usually a clue to the problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable To access array in awk

Hi, i have the following code in which i am passing array tldn in awk using -v option & despite of that condition is not getting matched,can somebody suggest how to handle shell arrays in awk tcount=(9875 9667) awk -F"\t" -v ltldn="${tldn}" 'NR==FNR {POSTPAIDMDNS=$2"|"$3;next} ... (6 Replies)
Discussion started by: siramitsharma
6 Replies

2. Shell Programming and Scripting

problem in access in array variables

hi all, its me again!!! i've requirement like this: i want to create a file & an array with its name having the filename as its substring. here is the test script!! #!/bin/bash touch $1 declare -a $1_rec; echo -n "$1_rec: " read $1_rec; echo $]; now see output: this is when i enter... (7 Replies)
Discussion started by: tprayush
7 Replies

3. Solaris

Unable to access 3500 FC array. Where is the problem?..

Hi, I have two Sun Fire V490 with Solaris 10 5/08, FC switch and two Sun StorageTek 3500 FC arrays. Each array is connected to switch and to one server at a time. In the last week I installed Solaris 10 5/08 on both servers and set up Sun Cluster, version 3.2. At wednesday all was fine - all... (7 Replies)
Discussion started by: Sapfeer
7 Replies

4. Shell Programming and Scripting

bin program access

Hello, I was wondering if I have something in my bin dir and I want to access it from another directory to make a change how can I go about it. Thank you. (7 Replies)
Discussion started by: gingburg
7 Replies

5. Programming

access variable through program stack

I am working on garbage collector in C? How should :confused: I find the part of heap where the variable are stored. It there any compiler (GCC) support for this. (2 Replies)
Discussion started by: amit gangarade
2 Replies

6. Shell Programming and Scripting

array access in END block failure

Hi guys i am new to shell scripting. I wrote this script that simply searches a column value of file1 from file2. please look at the code below: awk ' FILENAME==ARGV { file_1_data=$0; next } FILENAME==ARGV { file_2_data=substr($3,1,12); next } END { ... (5 Replies)
Discussion started by: fahadaizaz
5 Replies

7. UNIX for Dummies Questions & Answers

Access value outside awk or split value of array

Hello I am new to Unix. Please help me out. My Scenario: I am first collecting all the file names present in the directory with structure myinfo/yourinfo/supplierinfo I have four files with the names myCollector.java, yourCollector.java, someCollector.java, everyCollector.java. in the directory.... (1 Reply)
Discussion started by: jason.bean
1 Replies

8. Programming

Accessing microsoft access from C program

I have read a number of references to libraries that could be linked into a C program to access various databases. I have been tasked with writing an oracle library that would be able to access an Microsoft access database. The oracle database is running on a Unix server and would have to access... (2 Replies)
Discussion started by: beilstwh
2 Replies

9. Shell Programming and Scripting

How to access the C program variables in shell script

hi I wanted to access the C program variables in shell script. This script is called from the same C program. What are the ways in which i can access variables thankx (3 Replies)
Discussion started by: bhakti
3 Replies

10. Programming

C program with Oracle database access

Hey, I want to access oracle database through Unix C programming.. Can you through me some light on that... (5 Replies)
Discussion started by: kavi
5 Replies
Login or Register to Ask a Question