Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c programming multi-demensional char * array

Posted on 2011-09-03
3
Medium Priority
?
252 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 85

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 85

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

886 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