In an assignment for class we have to ask the user how many characters they'd like to enter and then read the characters into an array of that size. I've had problems with calling a function to do this. I will also be passing this array to other functions, so I'm not sure if anything has to be modified either.

This is what I've done so far

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

void Test(char, int);

int main()
{
    char *text;
    int i, size, *sizeptr;
    sizeptr = &size;
    Test(text, size);
    for(i = 0; i < size; i++)
    {
        putchar(text[i]);
    }
    system("PAUSE");
    return(0);
}

void Test(char *text, int *sizeptr)
{
    int i;
    printf("How many characters do you want to enter?\t");
    scanf("%d", sizeptr);
    text = (char*) malloc(*sizeptr * sizeof(char));
    memset(text, 0, *sizeptr);
    printf("Enter your text: (DO NOT INCLUDE SPACES)\n");
    scanf("%s", text);
}

Edited 7 Years Ago by kgomes: n/a

In an assignment for class we have to ask the user how many characters they'd like to enter and then read the characters into an array of that size. I've had problems with calling a function to do this. I will also be passing this array to other functions, so I'm not sure if anything has to be modified either.

This is what I've done so far

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

void Test(char, int);

int main()
{
    char *text;
    int i, size, *sizeptr;
    sizeptr = &size;
    Test(text, size);
    for(i = 0; i < size; i++)
    {
        putchar(text[i]);
    }
    system("PAUSE");
    return(0);
}

void Test(char *text, int *sizeptr)
{
    int i;
    printf("How many characters do you want to enter?\t");
    scanf("%d", sizeptr);
    text = (char*) malloc(*sizeptr * sizeof(char));
    memset(text, 0, *sizeptr);
    printf("Enter your text: (DO NOT INCLUDE SPACES)\n");
    scanf("%s", text);
}

Right away I see some problems

void Test(char, int);

void Test(char *text, int *sizeptr)

Alright, so I've managed to get it to compile. However the program crashes when I enter a string.

This is what I've got now:

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

void Test(char *text, int *sizeptr);

int main()
{
    char *text;
    int i, size, *sizeptr;
    sizeptr = &size;
    Test(text, sizeptr);
    for(i = 0; i < *sizeptr; i++)
    {
        putchar(text[i]);
    }
    system("PAUSE");
    return(0);
}

void Test(char *text, int *sizeptr)
{
    int i;
    printf("How many characters do you want to enter?\t");
    scanf("%d", sizeptr);
    text = (char*) malloc(*sizeptr * sizeof(char));
    memset(text, 0, *sizeptr);
    printf("Enter your text: (DO NOT INCLUDE SPACES)\n");
    scanf("%s", text);
}

You have two options, really. You can either pass in a pointer to char the pointer that you're passing in, or you can return the allocated array, or indeed both, if you really wanted. So...

//...
char *Test1(int *sizeptr){
  char *text = malloc( *sizeptr );
  // ...
  return text;
}
void Test2( char **text, int *sizeptr ) {
  *text = malloc( *sizeptr );
}
int main() {
  char *t1, *t2;
  int size = 12;

  t1 = Test1(&size)
  Test2( &t2, &size );

  free( t1 );
  free( t2 );
   return 0;
}

Not checked, compiled, built or tested, but it should give you an idea of how to do this.

Edited 7 Years Ago by twomers: n/a

I have this much modified example that you could use for an example:

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

char* Test();

int main()
{
	char *str;
	str = Test();
	fputs(str, stdout);

	return(0);
}

char* Test()
{
	int val = 0;
	char *s;

	printf("How many characters do you want to enter->");
	scanf("%d", &val);
	getc(stdin);

	s = (char*) malloc(val * sizeof(char));

	printf("Enter your text: (DO NOT INCLUDE SPACES)->");
	fgets(s, val, stdin);

	return s;
}

Edited 7 Years Ago by gerard4143: n/a

Or this one which is a little more exotic - the language lawyers should love this one

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

void Test(char **cptr);

int main()
{
	char *str;
	Test(&str);
	fputs(str, stdout);
	free(str);
	return(0);
}

void Test(char **cptr)
{
	int val = 0;

	printf("How many characters do you want to enter->");
	scanf("%d", &val);
	getc(stdin);

	*cptr = (char*) malloc(val * sizeof(char));

	printf("Enter your text: (DO NOT INCLUDE SPACES)->");
	fgets(*cptr, val, stdin);
}
This article has been dead for over six months. Start a new discussion instead.