Solved

c programming multi-demensional char * array

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

760 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

18 Experts available now in Live!

Get 1:1 Help Now