Solved

c programming multi-demensional char * array

Posted on 2011-09-03
3
245 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 177
libcurl - taking long time on curl_easy_perform 11 404
Inorder binary search tree 5 163
How to align numbers in C using the %d 2 96
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

770 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