0

I am currently trying to develop a neural network program for pattern recognition. The basis for the neuron are sigmoid neurons, and the network has three layers. At the moment I am trying to develop the first layer, which is an input layer. The code I have at the moment takes it a array of characters I have defined and tries to allocate one character per node. TO MAKE THAT CLEAR, the function I am trying to write takes in one value from the array i.e. the character at array[0] and assign it to a new nodes' input value.

Now there are 64 values in my array[], and there fore I want 64 nodes, with each node only containing one of the values from the array.

TLDR

My issue is, when i try to assign a single value from my array to a new node the struct array will not copy the value. therefore when I print this value the out is (NULL). Why is this happening? By the way I am fairly new to dynamic memory allocation.

Any help is much appreciated!!

Thank you!

Here is the function where it takes an input char array and assigns each character to a new node.

input *input_array = malloc(sizeof(input)*SIZE);
int counter = 0;
char input_char;
while (counter < (SIZE)){
    input_char = array[counter];
    input_array[counter].input_value='a';
    counter++;
    printf("%c, %c at %d\n",array[counter], input_array[counter].input_value, counter);

}
return input_array;

The struct looks like this

typedef struct input_node {
    char input_value;     
} input;

1 Answer 1

1

input_array is an array of pointers to inputs — input_array[counter] evaluates to an input * not an input. accessing input_value through input_array[counter] using the . operator is not actually the correct way to do that. you need to allocate memory for each of these pointers first maybe using malloc. see the short on structs for more on how you can access input_value through a pointer: https://youtu.be/EzRwP7NV0LM

1
  • Haha I change the while loop to a for loop... For some reason it works perfectly fine... Commented Jun 17, 2015 at 1:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .