I need a solution for a simple C program

Status
Not open for further replies.

evo1986

Newbie level 5
Joined
Dec 4, 2005
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,328
help for c assignment

i will be hapy if i can take a quick reply(solution) to that simple c program i am looking for different approachs. Pls pay attention...
 

Re: help for c assignment

i dont understand... does the modification mean that all letters are replaced with zeroes?

anyway, the approach i would take would be:

Code:
#include <stdio.h>

int main(int nArgCount, char * cpArgv[])
{
    char *** cpNameBuffer = NULL;

    cpNameBuffer = (char ***)calloc(10, sizeof(char **));
    if (cpNameBuffer == NULL) { return 0; } //you can report error if you want to

    for (unsigned short unName = 0; unName < 10; unName++)
    {
        cpNameBuffer[unName] = (char **)calloc(2, sizeof(char *));
        if (cpNameBuffer[unName] == NULL) { return 0; } //report error if you want to

        printf("Student #%i (First): ", unName + 1);
        cpNameBuffer[unName][0] = GetName(); //get first name
        printf("Student #%i (Last):  ", unName + 1);
        cpNameBuffer[unName][0] = GetName(); //get last name
    }

    //now you have your array of names, use it here for whatever operations you want

    return 1;
}

char * GetName(void)
{
    char * cpBuffer = NULL;
    char * cpCPtr = NULL;

    cpBuffer = (char *)malloc(256); //allocate 256 chars for temporary buffer
    if (cpBuffer == NULL) { return NULL; } //you can report error if you want to
    memset(cpBuffer, 0, 256); //set all to null

    if (gets(cpBuffer) == NULL) { return NULL; } //you can report an error here
    //sorry but gets() doesnt have a limit string size, but 256 chars chould be enough

    cpCPtr = strchr(cpBuffer, '\n'); //string is \n terminated, find the \n
    cpCPtr[0] = '\0' //and null terminate it
    cpCPtr = NULL;

    cpCPtr = (char *)malloc(strlen(cpBuffer) + 1); //alloc final char array
    if (cpCPtr == NULL) { return NULL; } //you can report error if you want to

    strcpy(cpCPtr, cpBuffer);
    return (cpCPtr);
}

i might have made a typo somewhere, please bear with me
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…