Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c programming multi-demensional char * array

Posted on 2011-09-03
3
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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.

704 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