Problem with passing array of pointers to struct among functions in C

Posted by karatemonkey on Stack Overflow See other posts from Stack Overflow or by karatemonkey
Published on 2010-04-25T18:15:51Z Indexed on 2010/04/25 18:23 UTC
Read the original article Hit count: 150

Filed under:

The Code that follows segfaults on the call to strncpy and I can't see what I am doing wrong. I need another set of eyes to look it this. Essentially I am trying to alloc memory for a struct that is pointed to by an element in a array of pointers to struct.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_POLICY_NAME_SIZE 64
#define POLICY_FILES_TO_BE_PROCESSED "SPFPolicyFilesReceivedOffline\0"

typedef struct TarPolicyPair
{
  int AppearanceTime;
  char *IndividualFile;
  char *FullPolicyFile;
} PolicyPair;


enum {
    bwlist = 0,
    fzacts,
    atksig,
    rules,
    MaxNumberFileTypes
  };


void SPFCreateIndividualPolicyListing(PolicyPair *IndividualPolicyPairtoCreate )
{
  IndividualPolicyPairtoCreate = (PolicyPair *) malloc(sizeof(PolicyPair));
  IndividualPolicyPairtoCreate->IndividualFile = (char *)malloc((MAX_POLICY_NAME_SIZE * sizeof(char)));
  IndividualPolicyPairtoCreate->FullPolicyFile = (char *)malloc((MAX_POLICY_NAME_SIZE * sizeof(char))); 

  IndividualPolicyPairtoCreate->AppearanceTime = 0;
  memset(IndividualPolicyPairtoCreate->IndividualFile, '\0', (MAX_POLICY_NAME_SIZE * sizeof(char)));
  memset(IndividualPolicyPairtoCreate->FullPolicyFile, '\0', (MAX_POLICY_NAME_SIZE * sizeof(char)));
}

void SPFCreateFullPolicyListing(SPFPolicyPair **CurrentPolicyPair, char *PolicyName, char *PolicyRename)
{              
  int i;

  for(i = 0; i < MaxNumberFileTypes; i++)
    {
      CreateIndividualPolicyListing((CurrentPolicyPair[i]));
      // segfaults on this call
      strncpy((*CurrentPolicyPair)[i].IndividualFile, POLICY_FILES_TO_BE_PROCESSED, (SPF_POLICY_NAME_SIZE * sizeof(char)));

    }
}

int main()
{
  SPFPolicyPair *CurrentPolicyPair[MaxNumberFileTypes] = {NULL, NULL, NULL, NULL};
  int i;

  CreateFullPolicyListing(&CurrentPolicyPair, POLICY_FILES_TO_BE_PROCESSED, POLICY_FILES_TO_BE_PROCESSED);

  return 0;
}

© Stack Overflow or respective owner

Related posts about c