C error "subscripted value is neither array nor pointer" - c

I have been having some problems with the segment of code below.
#include "stm32f0xx_tim.h"
#include "stm32f0xx_adc.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_conf.h"
#include "adc.h"
void calcSensor(float voltage1, float voltage2, int X, int Y)
{
float Iload = 0;
float Vsensor = 0;
float Rsensor = 0;
float Vdrop = voltage1 - voltage2;
uint32_t resistance = 0;
Iload = Vdrop/Rload;
Vsensor = Vin - Iload*Rmux - Iload*Rdemux-Vdrop;
resistance = Vsensor/Iload;
Rsensor[1][5] = resistance;
Y++;
if (Y == 22)
{
Y = 0;
X++;
if (X == 44)
{
X = 0;
}
}
}
void initRArray(void)
{
int x;
int y;
for(x = 0; x < 44; x++)
{
for(y = 0; y < 22; y++)
{
Rsensor[x][y] = 0;
}
}
}
The error comes the line:
Rsensor[1][5] = resistance;
The error is the same as the title:
subscripted value is neither array nor pointer
I originally had X and Y for indicies but have switched to 0 and 5 thinking it may have been an issue. That did not fix it. Additionally, I have the intRarray function which sets all values to 0. This array compiles fine, and it is using the same array that is having issues.
Below is the declaration of the array in the header file.
unsigned long int Rsensor[44][22];

You have a local variable float Rsensor = 0; which shadows the global array. Rename one of the two.

You have the following declaration in the program
float Rsensor = 0;
This makes Rsensor a float variable, not an array.

Related

Implicit declaration C

I wish to get my code cleaner, the code can compile, but unfortunately there are still some
stuff showing minor problem about this following "error message"
how can I solve this ?
#include <stdlib.h>
#include <stdio.h>
int main(){
int arr0[] = {1,2,3,4,5};
int arr1[] = {2,2,2,2,2};
int arr2[] = {1,4,2,4,4};
int sizeArr0 = sizeof(arr0);
int sizeArr1 = sizeof(arr1);
int sizeArr2 = sizeof(arr2);
parseArray(arr0[0], sizeArr0);
parseArray(arr1[0], sizeArr1);
parseArray(arr2[0], sizeArr2);
}
int parseArray(int ch[], int sizeValue){
int sum;
for(int x = 0; x < ch; x++){
int ch[x];
if(x == 5){
sum += 5;
}
if (sum == 15){
return sum;
}
}
}
warning: implicit declaration of function ‘parseArray’ [-Wimplicit-function-declaration]
17 | parseArray(arr0[0], sizeArr0);
| ^~~~~~~~~~
test.c: In function ‘parseArray’:
test.c:30:22: warning: comparison between pointer and integer
30 | for(int x = 0; x < ch; x++){
You need to have a function definition or prototype before the function which calls it
int parseArray(int ch[], size_t sizeValue);
int main()
{
/* ... */
for(int x = 0; x < ch; x++){ makes no sense and I believe that an typo.
for(size_t x = 0; x < sizeValue; x++){
int sizeArr0 = sizeof(arr0); is giving you the size of the array in char not in element types. You need to divide it by the size of the elements. It should also have different type (size_t) size_t sizeArr0 = sizeof(arr0) / sizeof(arr0[0]);
All local function variables have to be initialized as they are not zeroed as global variables. int sum = 0;
You pass the first element to the array not the reference to the array parseArray(arr0, sizeArr0); or parseArray(&arr0[0], sizeArr0);

Why does my factorial program return 0 in C?

so I am writing my first program in C and I'm trying to write a factorial function but it doesn't seem to be working and I don't know why.
#include <stdio.h>
int x = 5;
int counter;
int factorial (int x)
{
int counter = 1;
for ( x > 0; x<= 100;)
counter = counter * x;
x = x - 1;
}
int main (int factorial)
{
printf ("%i", counter);
}
So yeah don't exactly know why this doesn't work? Any help :)
updated answer
//int x = 5; this isn't doing anything
//int counter; not doing anything
int factorial(int x)
{
int counter = 1;
/*
for (x > 0; x <= 100;)
counter = counter * x;
1) x is input, don't use as counter, not in this case anyway
2) The variable 'counter' is where x is supposed to be
3) the loop is infinite
*/
x = 1; //initialize x
for (counter = 1; counter <= x; counter++)
x = x * counter;
//x = x - 1; shouldn't be here
//x has to be returned
return x;
}
//int main(int factorial) //don't put random arguments in main
int main()
{
//call the function
printf("%i", factorial( 5 ));
return 0;
}
Basically you are not calling the function so initial value of counter which is a garbage value would be printed out.

Hopalong fractals, checking c synatx

I have written following code in order to produce simple list of double pairs to import in plot program.
#include <stdio.h>
#include <math.h>
int main(void)
{
int i;
double x=2,y=3;
for(i = 0; i < 1000; i++){
x = y- x/fabs(x)*sqrt(fabs(x+0.7));
y = 0.3-x;
printf("%5.4f , %5.4f\n" ,x,y);
}
return 0;
}
I don't get what I expect from this functions. Instead of hopalong fractal I get linear progression graph. Is this only syntax error?
When you assign y, you use the new value of x, which has just been updated. The calculation requires the x value from the last step. Make a copy and use that:
int main(void)
{
double x = 2;
double y = 3;
int i;
for(i = 0; i < 1000; i++) {
double xx = x;
x = y - x/fabs(x)*sqrt(fabs(x + 0.7));
y = 0.3 - xx;
printf("%5.4f , %5.4f\n" ,x,y);
}
return 0;
}

segmentation fault

I am trying get a mandelbrot image clearly with the sequential programming in C++, but I am getting a segmentation fault during runtime. I have no idea about the seg. fault, but my program is perfectly compiling with no errors.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int file_write(unsigned int width, unsigned int height)
{
unsigned int **color = NULL;
FILE *fractal = fopen("mandelbrot_imageSequential.ppm","w+");
if(fractal != NULL)
{
fprintf(fractal,"P6\n");
fprintf(fractal,"# %s\n", "Mandelbrot_imageSequential.ppm");
fprintf(fractal,"%d %d\n", height, width);
fprintf(fractal,"40\n");
int x = 0, y = 0;
unsigned int R = 0, G = 0, B = 0;
for(x = 0; x < width; ++x)
{
for(y = 0; y < height; ++y)
{
R = (color[y][x]*10);
G = 255-((color[y][x]*10));
B = ((color[y][x]*10)-150);
if(R == 10)
R = 11;
if(G == 10)
G = 11;
if(B == 10)
B = 11;
putc(R, fractal);
putc(G, fractal);
putc(B, fractal);
}
}
fclose(fractal);
}
return 0;
}
int method(int x, int y, int height, int width, double min_re, double max_re, double min_im, double max_im, int max_iterations)
{
double threshold = 4;
double x_factor = (max_re-min_re)/(width-1);
double y_factor = (max_im-min_im)/(height-1);
double c_im = max_im - y*y_factor;
double c_re = min_re + x*x_factor;
double Z_re = c_re, Z_im = c_im;
unsigned int col = 0;
for(unsigned n = 0; n < max_iterations; ++n)
{
double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
if(Z_re2 + Z_im2 > threshold)
{
col = n;
break;
}
Z_im = 2 * Z_re * Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
}
return col;
}
int main(int argc, char *argv[])
{
unsigned int width;
unsigned int height;
unsigned int max_iterations;
unsigned int **color = NULL;
int x,y;
double threshold;
double min_re;
double max_re;
double min_im;
double max_im;
unsigned int NUM_OF_THREADS;
if(argc != 10)
{
printf("There is an error in the input given.\n");
return 0;
}
else
{
height = atoi(argv[1]);
width = atoi(argv[2]);
max_iterations = atoi(argv[3]);
min_re = atof(argv[4]);
max_re = atof(argv[5]);
min_im = atof(argv[6]);
max_im = atof(argv[7]);
threshold = atoi(argv[8]);
NUM_OF_THREADS = atoi(argv[9]);
}
color = (unsigned int**)malloc(height*sizeof(unsigned int*));
printf("height = %d\twidth = %d\tmaximum_iterations = %d\tminimum_x-value = %.2f\tmaximum_x-value = %.2f\tminimum_y-value = %.2f\tmaximum_y-value = %.2f\tthreshold_value = %.2f\tno. of threads = %d\t\n",height,width,max_iterations,min_re,max_re,min_im,max_im,threshold,NUM_OF_THREADS);
for(x = 0; x < height; x++)
{
color[x] = (unsigned int*)malloc(width*sizeof(unsigned int));
}
time_t ts,te;
time(&ts);
method(x,y,height,width,min_re,max_re,min_im,max_im,max_iterations);
time(&te);
double diff = difftime(te,ts);
file_write(width, height);
printf("Total Time elapsed: %f\n",diff);
return 0;
}
How to correct this segmentation fault?
At least one problem is in the file_write function.
unsigned int **color = NULL;
R = (color[y][x]*10);
I assume the color should be an input parameter.
If you are on Linux machine do the following :
$ulimit -c unlimited
Then run the code. Notice a core.[pid] file is generated. fire up gdb like following
$gdb ./your_app core.[pid]
It will take you the statement where segfault occurred. issue a "backtrace" command in gdb prompt to see the call hierarchy.
Remember compiling with "-g" flag to get more verbose gdb output.
There are two major problems with your code:
You allocate memory for the color array but then use a different color inside file_write() which is initialized to NULL.
You need to pass the first color as an argument to file_write():
int main(...)
{
...
file_write(color, width, height);
printf("Total Time elapsed: %f\n",diff);
return 0;
}
And declare the other color as an argument to file_write():
int file_write(unsigned int **color, unsigned int width, unsigned int height)
{
/* unsigned int **color = NULL; // Removed */
...
You're only calling method() once and not storing anything into color. You need to call it in a loop. Something similar to:
/* Untested */
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
color[y][x] = method(x,y,height,width,min_re,max_re,min_im,max_im,max_iterations);
}
}
Then, of course, you should check the return values of malloc(), fopen(), fprintf(), fclose(), ... , and check that the input variables have reasonable values and so on.
I also noticed that you're passing width and height in different order to file_write() and method(). To avoid future headaches, I would change the method() function to method(x, y, width, height) so that the horizontal and vertical arguments are passed in the same order.

What does int x; arr[100] mean exactly?

I'm tasked with changing a C program to x86 asm, and this line is confusing me:
int x; arr[100]
Full program:
int max = 100;
int val = 0x7a;
int x, arr[100]; /*I have a feeling that it is in fact a typo, and my compiler was just being nice*/
main(){
x = 1;/*I just got an email saying this was an error*/
for (x = 1; x <= max; x++){
arr[x] = val;
}
}
I think it means an array, but I'm not sure.
It is an array of int. Before C99 in certain circumstances the type could be omitted in a declaration and then int was assumed. (Note that you are missing a ; after the declaration of your array in your example).
For example:
const x = 10; // valid in C89, not in C99
auto y = 10; // valid in C89, not in C99
In old-school C, variable and functions are typed as int by default. This explains both the type of arr and why main() has no return type specified.
This just looks like buggy code:
int max = 100;
int val = 0x7a;
// int x; arr[100] // Syntax error
int x, arr[100]; // Declare x as a scalar int, arr as an array of 100 ints
main() {
// x = 1; // Unnecessary, as the for loop initializes x
// for (x = 1; x <= max; x++) { // Writing to arr[100] is an error
for (x = 0; x < max; x++) { // Loop thru indices of arr[]
arr[x] = val; // Set arr[0..99] to 0x7a
}
}

Resources