• C

newbie needs help on easy one

I am new to C and have written a simple program ( added below) that receives a string allocates address to a pointer
allocates memory to the string accordingly and is supposed to print the string. I think I did everythig right so why does it print garbage instead of the string ???

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char *ptr,str[20];
ptr = str;
      
      printf("Please enter a string  >:");
      gets(ptr);
      ptr = (char*)malloc(strlen(ptr)+1);
            if(ptr == NULL)
            {
            printf("memory allocation failure");
                  return;
            }
      printf("%s\n\n",ptr);
}

thanks
bitman
bitmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pagerbakCommented:
Hi bitman

Short answer: ptr points to some uninitialized memory, you just allocated!
It's hard to guess what you are trying to achieve, but to get a string and then print it, you could do this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[20];

printf("Please enter a string  >:");
gets(str); /* Better not be more than 19 bytes, to make room or the '\0' byte */
printf("%s\n\n",str);
}

0
bitmanAuthor Commented:
Sorry I have to reject as the idea of the exercise was to utilise pointers and memory allocation. I
manged to solve it by using strcpy

thanks all the same
Bitman
0
jjmcdCommented:
Instead of:

printf("Please enter a string  >:");
gets(ptr);
ptr = (char*)malloc(strlen(ptr)+1);


try:

ptr = (char*)malloc(200);
printf("Please enter a string  >:");
gets(ptr);
0
srinimrsCommented:
please try:
printf("Please enter a string  >:");
gets(*ptr);

i think it will work
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pagerbakCommented:
Don't ever use an uninitialized pointer, it will most certainly cause a segmentation violation.
If you want to play with malloc and strings you could try the following

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
      char      buf[256]; /* Must have some storage before gets() is called */
      char      *dups[10];      /* 10 pointers to a string */
      char       *string[10];      /* 10  more pointers to a string */
      int      i;
      for (i = 0; i < 10; i++) {
            printf("Please enter a string max 255 bytes >:");
            gets(buf);
            if ((string[i] = malloc(strlen(buf) + 1)) == NULL) {
                  perror("No more memory");
                  exit(1);
            }
            strcpy(string[i], buf);
            if ((dups[i] = strdup(buf)) == NULL) {
                  perror("No more memory");
                  exit(1);
            }
      }
      
      /* Print the strings */
      for (i = 0; i < 10; i++)
            printf("%s\n", string[i]);

      for (i = 0; i < 10; i++)
            printf("%s\n", dups[i]);

      printf("\n");
}

As shown the strdup() does the same as malloc() and strcpy() together.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.