When you declare an array like a[] = {...} the array gets a fixed size which cannot be altered, the size is determined when you compile.
If you want to use dynamic arrays you need to allocate on the heap, in C this is done with malloc and realloc. realloc allows you to resize an array.
e.g.
char* p = malloc(10);
char* q = realloc(p, 5); // now you made the array 5 bytes shorter
you should check the return value of realloc in order to know whether the realloc was successful.
char* q = realloc(p, 5);
if (q != NULL) // successful
ref: https://en.cppreference.com/w/c/memory/realloc
Answer from AndersK on Stack OverflowWhen you declare an array like a[] = {...} the array gets a fixed size which cannot be altered, the size is determined when you compile.
If you want to use dynamic arrays you need to allocate on the heap, in C this is done with malloc and realloc. realloc allows you to resize an array.
e.g.
char* p = malloc(10);
char* q = realloc(p, 5); // now you made the array 5 bytes shorter
you should check the return value of realloc in order to know whether the realloc was successful.
char* q = realloc(p, 5);
if (q != NULL) // successful
ref: https://en.cppreference.com/w/c/memory/realloc
You cannot do that, like @Klutt & @david-ranieri said in comments you can copy the content of b in a and then fill with 0.
But I recommend to use dynamic array, since C++11 you can do :
// initialise with braced-init-list
int* a = new double[8] { 1, 2, 3, 4, 5, 6, 7, 8};
int* b = new double[2] { 1, 6};
delete[] a;
a = b;
How can I overwrite an array of chars (AKA a string), with a new array of chars in C? - Stack Overflow
overwriting character arrays in C - Apple Community
Overwriting array in C - Stack Overflow
Fast way to replace elements in array - C - Stack Overflow
Use strncpy:
char chararray[6];
(void)strncpy(chararray, "abcdefgh", sizeof(chararray));
Use strcpy(char *destination, const char *source);.
#include <string.h>
int main(void) {
...
char A[32] = "00000000000000001111111111111110";
...
strcpy(A, "11111111111111111111111111111111");
}
Though safer is strncpy(char *destination, const char *source, size_t num), which will only copy num amount of characters, preventing going out of bounds on the destination:
#include <string.h>
int main(void) {
...
char A[32] = "00000000000000001111111111111110";
...
strncpy(A, "11111111111111111111111111111111", sizeof(A));
}
For your specific case where you initially have 0 and 1, the following might be faster. You'll have to bench mark it. You probably can't do much better with plain C though; you may need to dive into assembly if you want to take advantage of "x86 trickery" that may exist.
for(int i = 0; i < size ; i++){
array[i] *= 123456;
}
EDIT:
Benchmark code:
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
size_t diff(struct timespec *start, struct timespec *end)
{
return (end->tv_sec - start->tv_sec)*1000000000 + end->tv_nsec - start->tv_nsec;
}
int main(void)
{
const size_t size = 1000000;
int array[size];
for(size_t i=0; i<size; ++i) {
array[i] = rand() & 1;
}
struct timespec start, stop;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for(size_t i=0; i<size; ++i) {
array[i] *= 123456;
//if(array[i]) array[i] = 123456;
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
printf("size: %zu\t nsec: %09zu\n", size, diff(&start, &stop));
}
my results:
Computer: quad core AMD Phenom @2.5GHz, Linux, GCC 4.7, compiled with
$ gcc arr.c -std=gnu99 -lrt -O3 -march=native
ifversion: ~5-10ms*=version: ~1.3ms
For a small array such as your it's no use trying to find another algorithm, and if the values are not in a specific pattern a simple loop is the only way to do it anyway.
However, if you have a very large array (we're talking several million entries), then you can split the work into threads. Each separate thread handles a smaller portion of the whole data set.
sptr=malloc(sizeof(char*)*nStrings);
for(i=0;i<nStrings;i++)
{
scanf("%s",string);
sptr[i]=strdup(string);
}
I assume the variable string has enough memory to keep the read strings.
The error occured because you set the pointer to point to the string variable.
You need to allocate 1 character extra for the null terminator:
sptr[i]=malloc(sizeof(char)*(length+1));
Also, you need to copy the string into the newly allocated memory:
strcpy(sptr[i], string);
You're not storing strings you're storing characters so all you can read is one character so that'd be the S
My suspision is that the next character is an O so when you look at it as a string you get SO
printf("'%c'", items[i][j]);
You are storing characters and reading strings. Try reading character back from the Array.
Change your code to:
int i;
for(i = 0; i < rows; i++)
{
printf("[");
int j;
for(j = 0; j < columns; j++)
{
printf("'%c'", items[i][j]);
if(j != columns - 1)
printf(", ");
}
printf("]");
printf("\n");
}
It's an array of values, not of pointers. So you'd just do
array[5] = MyType();
This requires MyType to support the assignment operator.
Incidentally, there's rarely a need for manual array allocation like this in C++. Do away with the new and delete and use std::vector instead:
std::vector<MyType> array(10);
array[5] = MyType();
Note, there's no need to delete anything.
No.
The individual elements of the array were not new'd, just the array itself was.
array[5] = MyType(); // note no `new` here.