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

int main()
{
    char **pps;

    int i, n = 10;

    char name[256];

    pps = (char **)calloc(n,sizeof(char **));

    for (i=0; i<n; i++)
    {
        printf("\n Enter name[%d]: ",i+1);
        gets(name);
        printf("\n Name=%s len=%d",name,strlen(name)+1 );

        pps[i] = (char *)malloc(strlen(name)+1);
        printf("\n pps[i]=%u",pps[i]);
        if (pps[i] = NULL)
        {
            perror("\nerror in malloc");
            exit(1);
        }

        printf("\n pps[i]=%u",pps[i]);
        strncpy(pps[i],name,strlen(name)+1);
    }

}

/* input/output from program: Enter name[1]: abcdef

 Name=abcdef len=7
 pps[i]=13311184
 pps[i]=0

*/

The program gives Runtime Error ??? Please help to find what is wrong and why PPS[i] become currupted ??? I am using visual studio 2010

2
  • 1
    And don't put a printf between your failing malloc and the related perror. (printf may change errno and make the perror less than useless.) Commented Feb 13, 2016 at 18:22
  • Try to adjust warning level higher, because if (pps[i] = NULL) should give you a warning! Or if you already got a warning, then consider not ignoring the warnings, but actually fixing them... They're there for a reason! Commented Feb 13, 2016 at 19:18

1 Answer 1

4

Your problem is here:

if (pps[i] = NULL)

It should be:

if (pps[i] == NULL)

Remember that = is an assignment operator while == is comparation

Also remember to use free at the end of your program to avoid memory leak.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.