[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

newbie needs help on easy one

Posted on 2000-01-24
5
Medium Priority
?
186 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:bitman
5 Comments
 
LVL 1

Expert Comment

by:pagerbak
ID: 2383446
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
 

Author Comment

by:bitman
ID: 2390642
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
 
LVL 3

Expert Comment

by:jjmcd
ID: 2391069
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
 

Accepted Solution

by:
srinimrs earned 0 total points
ID: 2394180
please try:
printf("Please enter a string  >:");
gets(*ptr);

i think it will work
0
 
LVL 1

Expert Comment

by:pagerbak
ID: 2394718
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

590 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