NO

Author Topic: Array do not have the correct Elements ?  (Read 7553 times)

John1

  • Guest
Array do not have the correct Elements ?
« on: December 17, 2018, 10:43:19 PM »
Hello,

I´m quite new to C and try to learn. After some time of trying different codes, I decided to post here. Among various Problems in the Program, I will start with one. Sorry for my english.

The Program gets the input from fgets fgets(car,sizeof(car),stdin) and give the value to an Array char* carA[100]; with carA[z++] = car; .

Strange is that the Index is incrementing, but the Values at the Output are not correct :

Code: [Select]
void listCars(void) {
          for(int i = 0; i <= (int)strlen(*carA)-1; i++) {
  printf("Cars rented : %s\n", carA[i]);
 }
}

« Last Edit: December 18, 2018, 05:04:34 PM by John1 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Array do not have the correct Elements ?
« Reply #1 on: December 18, 2018, 02:05:56 PM »
Your array structure is unknown, so hard find errors, and this is weird for me:
Code: [Select]
i <= (int)strlen(*carA)-1;the lenght of the first string array element - one ?

an example with a static string array
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int __cdecl main(void)
{
char buf[80], astr[10][100]; // an array of strings
int cnt = 0; // string counter
while(fgets(buf,sizeof(buf),stdin)) { // read to buffer, not really needed with a static string array
if (*buf == 10 || cnt >= 10) break; // exit if plain ENTER or array limit
int len = strlen(buf);
buf[len-1] = 0; // remove LF
strcpy(astr[cnt], buf); // copy to array
cnt++; // advance next
}
for (int i=0; i<cnt; i++)
printf("%d %s\n", i, astr[i]);
return 0;
}
May the source be with you

John1

  • Guest
Re: Array do not have the correct Elements ?
« Reply #2 on: December 18, 2018, 05:31:44 PM »
Hello TimoVJL,

Thank you for your answer.

I use char* carA[100]; to declare the Array.
I changed i <= (int)strlen(*carA)-1;  to i < z; .

I tried a bit to change my Code similar to your Example, but I couldn´t let it run, due an Exception 255. To not copy and paste your Code and learn something.

Here is the Code I use to add Elements to the Array, maybe you may see some Errors.

Code: [Select]
void addNewCar(void) {
 char* car = (char*)malloc(100*sizeof(char));
 printf("Add a new Car : ");
 if(fgets(car,sizeof(car),stdin) == NULL) {
  printf("Wrong Input\n");
  dump_buffer(stdin);
  free(car);
  return;
 }
 carA[z++] = car;
 free(car);
 return;
}
« Last Edit: December 18, 2018, 05:36:37 PM by John1 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Array do not have the correct Elements ?
« Reply #3 on: December 18, 2018, 06:28:35 PM »
couple things:
1. use if (fgets(car, 100, stdin) == NULL) as sizeof(car) is a pointer size.
2. don't free memory in addNewCar(), do it end of program
3. use tempory buffer for input and malloc only needed memory and copy that string to it.
May the source be with you

John1

  • Guest
Re: Array do not have the correct Elements ?
« Reply #4 on: December 18, 2018, 10:15:15 PM »
I changed it following the first two advises.

Quote
3. use tempory buffer for input and malloc only needed memory and copy that string to it.
Sorry I couldn´t understand the third advise ?

Actually it puts still the wrong Elements in the Array. Input: Car one - Car two - Car three

Code: [Select]
void addNewCar(void) {
 car = (char*)malloc(100*sizeof(char));
 printf("Add new Car : ");
 if(fgets(car,100,stdin) == NULL) {
  printf("Wrong Input\n");
  dump_buffer(stdin);
  free(car);
  return;
 }
 carA[z++] = car;
 return;
}

Output :

Element : Car one and  Index : 0
Rented Car : Car one

Element : Car three and  Index : 1
Rented Car  : Car three

Element : Car three and  Index : 2
Rented Car : Car three
« Last Edit: December 18, 2018, 10:18:10 PM by John1 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Array do not have the correct Elements ?
« Reply #5 on: December 19, 2018, 12:28:31 AM »
Something like this:
Code: [Select]
int addNewCar(void)
{
char buf[100];
printf("Add a new Car : ");
if (fgets(buf, 100, stdin) == NULL || *buf == 10)
{
printf("Wrong Input\n");
return 0;
}
int len = strlen(buf);
buf[len-1] = 0;
char *car = (char *)malloc(len);
strcpy(car, buf);
carA[z++] = car;
return len;
}
« Last Edit: December 19, 2018, 11:09:00 AM by TimoVJL »
May the source be with you

John1

  • Guest
Re: Array do not have the correct Elements ?
« Reply #6 on: December 19, 2018, 02:31:38 PM »
I tired this code but the output is still wrong. I tried also strcpy(carA[z++], buf); without success.

Why the Array doesn´t get the correct Elements ?

Thank you

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Array do not have the correct Elements ?
« Reply #7 on: December 19, 2018, 03:10:20 PM »
A test code:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* carA[100];
int z;

int addNewCar(void)
{
char buf[100];
printf("Add a new Car : ");
if (fgets(buf, 100, stdin) == NULL || *buf == 10)
{
printf("Wrong Input\n");
return 0;
}
int len = strlen(buf);
buf[len-1] = 0;
char *car = (char *)malloc(len);
strcpy(car, buf);
carA[z++] = car;
return len;
}

void listCars(void)
{
for (int i = 0; i < z; i++)
printf("Cars rented : %s\n", carA[i]);
}

int main(void)
{
while (z < 100 && addNewCar());
listCars();
return 0;
}
May the source be with you

John1

  • Guest
Re: Array do not have the correct Elements ?
« Reply #8 on: December 19, 2018, 04:31:56 PM »
Timo, thank you for your patience. Now it works  :)

I changed the the malloc to car = (char*)malloc(100*sizeof(len)); before sizeof was with char. Syntax error, maybe because char have more bits and create an undefined behaviour ?

May you give me an advise on how to make the programs data persistent after quitting the Program, I thought to Files or Database ?

Have a good evening
John




Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Array do not have the correct Elements ?
« Reply #9 on: December 19, 2018, 05:11:53 PM »
I changed the the malloc to car = (char*)malloc(100*sizeof(len));
Not a good idea, as len is a correct size for that ANSI string. Unicode needs more room, but fgets() was used.
A fgets can read strings from file and fputs can write them.
May the source be with you