Extend an array

Trying to extend an array by creating a new array and filling that array with values from the previous one PLUS the new value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  int count = 2;
  long int y;
  char array[1];
  array[0] = 2;
  int length = 1;
  
  for (y = 5; count < 10002 ; y+=2)
  {
    if (isPrime(array, length, y))
    {
	count+=1;
	char array2[length + 1];
	
	for (int J = 0; J < length; J++)
	array2[J] = array[J];
	
	array2[length] = y;
	
	array = array2;//<== error points here
	
	length += 1;
    }
  }


Always get this message when I compile:

:39:10: error: incompatible types in assignment of ‘char [(((long unsigned int)(((long int)(length + 1)) + -0x00000000000000001)) + 1)]’ to ‘char [1]’


What does the message mean and how do I fix it?
Last edited on
You need to use dynamically allocated arrays.
How do I use dynamically allocated arrays?

E: nvm, looking it up. Thanks
Last edited on
Okay so I tried the dynamically allocated array thing and it is working now, however another problem arises. I will post the entire code here for reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>

bool isPrime(char[], int, int);
void dealloc_arr(char[]);
char* extend_arr(char [], int, int);

int main()
{
  int count = 1;
  int y;
  int length = 1;
  char* array = new char[length];
  char* temp;
  array[0] = 2;
  
  for (y = 3; count < 10001 ; y+=2)
  {
    if (isPrime(array, length, y))
    {
	count+=1;
	temp = extend_arr(array, length, y);
	if (temp == NULL)//<== problem is here
	{
	  printf("%d ", count);
	  fputs("could not extend the array\n", stderr);
	  exit (2);
	}
	else
	{
	  array = temp;
	}	
	length += 1;
    }
    
  }
  printf("%d\n", y);
  return EXIT_SUCCESS;
}


bool isPrime(char arr[], int size, int val)
{
  char *ptr = arr;
  for (int U = size; U > 0 ; U--)
  {
    if ((val / *ptr) < 3)
    {
      ptr++;
      continue;
    }
    if (val % *ptr == 0)
      return false;
    
    ptr++;
  }
  return true;
}

void dealloc_arr(char arr[])
{
  delete[] arr;
  arr = NULL;
}

char* extend_arr(char arr[], int length, int val)
{
  char *array2 = new char[length + 1];
  int N = strlen(arr);
  if (N == length)
  {
    for (int J = 0; J < length; J++)
      array2[J] = arr[J];
    
    array2[length] = val;
    
    dealloc_arr(arr);
  }
  else
  {
    return NULL;
  }
   return array2;

}


My problem is that when I run this, at about the 25th prime it stops because the extend_arr is returning NULL. And this is because the length of the array sent to extend_arr is not matching up with 'length' variable.

Can anyone see a problem here?

$ ./Large_Primes  
could not extend the array
25 
Last edited on
Line 71 seems bogus to me. You don't ever null terminate your array so strlen cannot be expected to work.
Is there a specific reason (sadistic teacher?) you're using dynamically-allocated arrays and not strings or vectors?
Last edited on
@Cubbi, I was trying not to use dynamically allocated arrays, but I got a response above (@helios) that said I should try using that. I haven't used strings in such a while now, I don't know if I could go back to using them. I prefer char. This is just a project I am working on by myself, we are in break.

@firedraco, I don't know what you mean by "...don't ever null terminate your array..."
Last edited on
Couple things:
1. Let s[n] be the first element of s that is equal to 0. strlen(s) returns n, not the actual size of the array.
2. char typically overflows for values higher than 127. You'll want to use int or unsigned.

Since you're only initializing the first element of the array, because of my point #1 above, strlen() goes beyond the boundary of the array and returns a number that has nothing to do with the size of the array.
Remove the call to strlen() it does nothing for you because you already know the size of the array. You're passing it as a parameter. It's impossible for the array to be of a size different from that.
@helios, thanks man, you have been helpful
Topic archived. No new replies allowed.