This is my first time posting or really doing anything here on daniweb. I have read a lot of other posts and you all are very knowledgeable and respectful. So if I don't do something that I need to as per the forum regulations let me know. I have a question. I am writing a program for class that creates a basic vector function in c. It uses this vector function to dynamically expand an array to the size of the string that the user types in. My problem is when i call the function it says conflicting types for 'vector'. Can anyone help me with this problem. I have the code posted below.

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

int arraySize = 1;

int main(void){

	int i, j;
	int h = 0;
	int index = 0;
	char c;
	char *array = (char *) calloc(arraySize, sizeof(char));
	array[0] = '\0';
	char ch[] = "HW6 Input: ";
	printf("\nEnter a character to input in the string.\n");
	printf("Only enter one character at a time and press enter after each character.\n");
	printf("Press enter without a character to continue;\n");
	
	while( (c = getc(stdin)) != '\n'){

		array[index] = c;
		index++;
		if(index < arraySize){

			char *tmp = (char*) calloc(arraySize, sizeof(char));
			memcpy(tmp, array, arraySize);
			*array = vector(array);
			memcpy(array, tmp, arraySize);

		}	

	}

	arraySize++;
	*array = vector(array);
	
	index++;
	array[index] = '\0';

	char output[arraySize + 13];
	for(i = 0; i < strlen(ch); i++){

		output[i] = ch[i];

	}
	
	for(j = i; j < strlen(output); j++){

		
		output[j] = array[h];
		h++;

	}

	printf("\n%s\n", output);

	free(array);

	return 0;

}

char *vector(char *theArray){

	arraySize++;

	void *temp = (char *) calloc(arraySize, sizeof(char));

	theArray = (char *) temp;
		
	return theArray;

}

Thank you.

Edited 6 Years Ago by y9john: Added more information

Not really sure what the objective is here? If you want to allocate memory via a function try the following code:

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

bool vector(char **theArray, int size);

int main()
{
	int i = 0;
	char *ch = (char*)NULL;

	if (!vector(&ch, 100))/*call vector and test for failure*/
	{
		fputs("Allocation failed...exiting program\n", stderr);
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < 26; ++i)/*load some values*/
		*(ch + i) = ('a' + i);

	*(ch + i + 1) = '\0';/*delimit our cstring with '\0'*/

	fprintf(stdout, "ch->%s\n", ch);/*display our cstring*/

	free(ch);

	ch = (char*)NULL;

	return 0;
}

bool vector(char **theArray, int size)
{
	*theArray = (char*)malloc(size * sizeof(char));
		
	if (!theArray)
		return false;

	return true;

}

Edited 6 Years Ago by gerard4143: n/a

Directly below is the requirements document and below that is my revamped code. It turns out I had my vector function below my main so it wasn't reading it right. It fixed that problem when I did it ... though now the array isn't loading anything.

Write a program that prompts the user for characters
(not strings!). As the user enters characters your
program must call the "vector function" (that you
must write) to automatically adjust the character
array size to hold the input.

The function must use malloc/calloc/realloc to
create/adjust the array size.

You may also want to read the man page on memcpy.

The function must return a pointer (to the array)
to the main routine (you will probably want to pass
in the array also).

You can adjust the array in chunks of 5 or 10 characters
(vs 1 character) if you want.

When the user enters "return" (in place of a character)
your program must append the '\0' character to the end
(making it a string). Your program must then append
the new string to the text/string "HW 6 input: "
(be carefull here). Your program must then print out
the resulting string.

SAMPLE RUN:
-----------
Enter characters: 1234567

HW 6 input: 1234567

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

int arraySize = 1;

char *vector(char *theArray){

	arraySize++;

	void *temp = (char *) calloc(arraySize, sizeof(char));

	theArray = (char *) temp;
		
	return theArray;

}

int main(void){

	int i, j;
	int h = 0;
	int index = 0;
	char c;
	char *array = (char *) calloc(arraySize, sizeof(char));
	array[0] = '\0';
	char ch[] = "HW6 Input: ";
	printf("\nEnter a character to input in the string.\n");
	printf("Only enter one character at a time and press enter after each character.\n");
	printf("Press enter without a character to continue;\n");
	
	while( (c = getc(stdin)) != '\n'){

		array[index] = c;
		index++;
		if(index < arraySize){

			char *tmp = (char*) calloc(arraySize, sizeof(char));
			memcpy(tmp, array, arraySize);
			array = vector(array);
			memcpy(array, tmp, arraySize);

		}	

	}

	arraySize++;
	array = vector(array);
	
	index++;
	array[index] = '\0';

	char output[arraySize + 13];
	for(i = 0; i < strlen(ch); i++){

		output[i] = ch[i];

	}
	
	for(j = i; j < strlen(output); j++){

		
		output[j] = array[h];
		h++;

	}

	printf("\n%c\n", array[0]);

	free(array);

	return 0;

}

I got the program working ... I had random syntax errors and misplaced items. Thank you gerard for your help. I appreciate it. Have a good day. If you want I can post the final code so you can see what I mean.

I did look at your code and when I ran it, it output the alphabet. The program that we are required to create needs the user to input characters and after they are done, they press enter and turn it into a string.

This article has been dead for over six months. Start a new discussion instead.