Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-08-2012, 04:51 PM   #1
Registered: May 2008
Distribution: Arch, Sabayon, Fedora, Ubuntu
Posts: 87

Rep: Reputation: 17
Post Problem with C program and pointers of unspecified size

This is what I have so far:
#include <stdio.h>
#include <stdlib.h>

void Copystring(char *in, char **out);
void Iput(char **iput);

void main()
char *input;
char *output;
printf("Enter input: ");
Copystring(input, &output);
printf("\nYou wrote: %s", output);

void Iput (char **iput)
int tmp;
*iput = malloc(sizeof(tmp));
while (tmp != '\n')
tmp = malloc(sizeof(int));
tmp = getchar();
while (*iput) *iput++;
*iput++ = tmp;
*iput = realloc(*iput, sizeof(*iput)+sizeof(char));

void Copystring(char *in,char **out)
*out = malloc(sizeof(in)+1);
*out = in;
I basically want a program that will accept any length of text and re-print it. I am aware that there are libraries already present with some of these functions in them, but I want to try it without using too many libraries. Every time I run this, I get segmentation faults and malloc.c errors, so I assume the problem is in my memory allocation.

I would write a simpler program that accepts up to 64 characters of input, but I want to make it a more flexible program.

I am just starting to learn C programming after learning some higher-level languages, so please point out any other problems I have in this program. Any help is appreciated. (sorry it is uncommented).
Old 07-08-2012, 06:29 PM   #2
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334
Originally Posted by kev717 View Post
Any help is appreciated.
The first thing to understand is that sizeof() is not a function that is executed at runtime; it is used by the compiler to deduce the size of the given data type, or the size of the data type for a given variable. For example, if you have a variable 'foo' that is declared to be an int, sizeof(foo) and sizeof(int) will return the same result (typically 4).

When using sizeof() on a pointer type/variable, the size of the pointer for your system is returned (4 for 32-bit arch, 8 for 64-bit arch), not then length of the data that is referenced by that pointer. For this reason, your application will fail, as I'm sure you already have discovered.

Consider the following for gathering input:
void Iput(char** iput)
    int tmp = 0;
    int len = 0;   /* overall length of the string that is read */

    if (iput == NULL)

    *iput = NULL;  /* always start with a clean slate */

    /* read from stdin until either a newline is read, or EOF is reached */
    while ((tmp = getchar()) != '\n' && tmp != EOF)
        /* allocate one additional byte for the input */
        *iput = realloc(*iput, ++len);

        /* store the inputted byte of data */
        (*iput)[len - 1] = tmp;

    /* finish by inserting a null-character at the end of the string */
    *iput = realloc(*iput, ++len);
    (*iput)[len - 1] = '\0';
For copying the string, as you have proposed in Copystring(), this is really unnecessary. However if you wish to pursue that function I can tell you that it will be very similar to the function shown above. The exception will be that you will be parsing the input until a null-character is found.

If you have additional questions, please feel free to ask.

Last edited by dwhitney67; 07-08-2012 at 06:32 PM.
2 members found this post helpful.
Old 07-08-2012, 07:01 PM   #3
Registered: May 2008
Distribution: Arch, Sabayon, Fedora, Ubuntu
Posts: 87

Original Poster
Rep: Reputation: 17
Thank-you for your assistance. It was very helpful.


cli, input, pointers, program

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Porting C Program to Linux & Problem with Size of ICMP Packet SeymourButts Programming 11 01-15-2010 09:06 AM
Program on Command Line Argument and Pointers thelink123 Programming 2 09-01-2009 10:05 AM
Program on pointers- explanation needed thelink123 Programming 4 07-18-2009 03:39 PM
Problem with font size on gtk based program eduac Linux - Software 1 03-21-2006 01:38 PM

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration