I'm writing a little program that must:
- Ask a user how many numbers he will enter (to stock in 2 tab dynamically)
- Enter numbers step by step and the number go in the bufferP if he is positive and in bufferN if negative
- If there is no more place in a buffer, double the size of this one
- Finally printf both buffers
Here is my code:
#include <stdlib.h>
#include <stdio.h>
int main(){
int length = 0;
printf("Number of data: \n");
scanf("%d", &length);
int *bufferP = (int*)malloc(2*sizeof(int));
int *bufferN = (int*)malloc(2*sizeof(int));
int number = 0;
for (int i = 0 ; i <= length ; i++){
scanf("%d", &number);
if(number < 0){
if(*bufferN == NULL){
printf("No more place");
exit(0);
}
*bufferN= number;
*bufferN++;
}
if(number >= 0){
if(*bufferP == NULL){
printf("No more place");
}
*bufferP = number;
*bufferP++;
}
}
int res =0;
printf("tab negative : ");
for (int i = 0; bufferN[i] != NULL; i++)
{
res = bufferN[i];
printf("%d\n", res );
}
printf("tab positive : ");
for (int i = 0; bufferP[i] != NULL; i++)
{
res = bufferP[i];
printf("%d\n", res );
}
}
I've got those errors :
In function ‘main’:
rev_S3_ptr.c:14:16: error: comparison between pointer and integer [-Werror]
if(*bufferN == NULL){
^~
rev_S3_ptr.c:19:4: error: value computed is not used [-Werror=unused-value]
*bufferN++;
^~~~~~~~~~
rev_S3_ptr.c:23:16: error: comparison between pointer and integer [-Werror]
if(*bufferP == NULL){
^~
rev_S3_ptr.c:28:4: error: value computed is not used [-Werror=unused-value]
*bufferP++;
^~~~~~~~~~
rev_S3_ptr.c:35:29: error: comparison between pointer and integer [-Werror]
for (int i = 0; bufferN[i] != NULL; i++)
^~
rev_S3_ptr.c:41:29: error: comparison between pointer and integer [-Werror]
for (int i = 0; bufferP[i] != NULL; i++)
So my question are:
- The buffer works like a tab right ? So why can't I just say that if the pointer is null it is full and do *buffer ++ ?
- At the "No more place" I must double the size of the buffer and I don't know how to do this.