Solved

c programming multi-demensional char * array

Posted on 2011-09-03
3
243 Views
Last Modified: 2012-05-12
Please tell me what I'm doing wrong here. I'm teaching myself and I'm not sure what the proper syntax is to implement this...

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#define HASX 0
#define NOX 1

#define MAXPAIRS 10 
#define DEM 2 

int main()
{
   char *mArray[DEM][MAXPAIRS];

   init_matrix( mArray ); 

   printf("Has x : %s\n", mArray[HASX][0]);
   printf("No x  :  %s\n", mArray[NOX][0]);

   printf("Has x : %s\n", mArray[HASX][1]);
   printf("No x  :  %s\n", mArray[NOX][1]);

   printf("Has x : %s\n", mArray[HASX][2]);
   printf("No x  :  %s\n", mArray[NOX][2]);

   printf("Has x : %s\n", mArray[HASX][3]);
   printf("No x  :  %s\n", mArray[NOX][3]);

   printf("Has x : %s\n", mArray[HASX][4]);
   printf("No x  :  %s\n", mArray[NOX][4]);
}



int init_matrix( char *mArray[DEM][MAXPAIRS] )
{
   add_pair(mArray, "A1", "B1"); 
   add_pair(mArray, "A2", "B2"); 
   add_pair(mArray, "A3", "B3"); 
   add_pair(mArray, "A4", "B4"); 
   add_pair(mArray, "A5", "B5"); 
}



int add_pair( char *mArray[DEM][MAXPAIRS], char *has_x, char *no_x )
{
   int i;
   for( i = 0; i <= MAXPAIRS; i++)
   {
       if ( mArray[HASX][i] == NULL )
       {
           mArray[HASX][i] = (char *)malloc(10 * sizeof(char));
           mArray[NOX][i] = (char *)malloc(10 * sizeof(char));
           strcpy(mArray[HASX][i], has_x);
           strcpy(mArray[NOX][i], no_x);
           
           //charArray[HASX][i] = strdup(has_x);
           //charArray[NOX][i] = strdup(no_x);

           break;
       }
   }
}

Open in new window

Results:
Has x : A1
No x  :  B1
Has x : A2
No x  :  B2
Has x : A3
No x  :  B3
Segmentation fault (core dumped)

Open in new window

0
Comment
Question by:kindaprog
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 36479277

  char *mArray[DEM][MAXPAIRS]; //mArray is allocated on the stack, and probably contains non-null garbage values
   memset(mArray,0,sizeof(mArray));  // will initialize it to be full of null values;
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 36479278
also for( i = 0; i <= MAXPAIRS; i++) should be  for( i = 0; i < MAXPAIRS; i++)
0
 

Author Closing Comment

by:kindaprog
ID: 36479455
Thank you! Great explanation.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now