hi i am new to c/c++

i am making a program for a project in class

i have only started the program it still has no content, only structures however when i compiled and run it in C

the first printf wont display in the .exe

there are no errors

here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef double mNum[10];
typedef int *mDim;

//structure for dimension
typedef struct{
       mNum **pX;//declares double pointer for matrix
       mDim m,n; //variable size of matrix
}structDimension;

//structure for run-time initialization of matrix
mNum *initializeMatrix(structDimension n){
     mNum **temp;
     int ctr;
     for(ctr=0 ; ctr<n ; ctr++)
{
          temp[ctr]=malloc(sizeof(int)*n);
}
     return temp[ctr];
}


int main (int args, char *argv[]){
    structDimension *pX, m, n;
    int ctr; //counter - initializing memory location
    int ctrrow, ctrcol;//counter for clearing matrix and displaying values
    int ans;
    
    m=1;
    n=1;
    for(ctrrow=0 ; ctrrow<m ; ctrrow++){
          for(ctrcol=0 ; ctrcol<n ; ctrcol++){
                       pX[ctrrow][ctrcol] = 0;
          }
    }
    printf("\n\nGood Day!\n\n");
        
getch ();
free(pX);    
    
}

i tried compiling it in .cpp and i got these errors:

....cpp: In function `double (* initializeMatrix(structDimension))[10]':
....cpp:18: error: no match for 'operator<' in 'ctr < n'

....cpp:20: error: no match for 'operator*' in '4u * n'
....cpp: In function `int main(int, char**)':
....cpp:32: error: no match for 'operator=' in 'm = 1'
....cpp:9: note: candidates are: structDimension& structDimension::operator=(const structDimension&)
....cpp:33: error: no match for 'operator=' in 'n = 1'
....cpp:9: note: candidates are: structDimension& structDimension::operator=(const structDimension&)
....cpp:34: error: no match for 'operator<' in 'ctrrow < m'

....cpp:35: error: no match for 'operator<' in 'ctrcol < n'
....cpp:36: error: no match for 'operator[]' in '*((+(((unsigned int)ctrrow) * 12u)) + pX)[ctrcol]'

i am using bloodshed, and the automatic file extension is always .cpp and whenever i have problems using C, i save it in .cpp and it would work. however this is the first time i encountered problems with "no match for operator"


PLEASE HELP

Dani AI

Generated

— the root cause here is type-and-pointer mismatch plus compiling under C++ rules. is correct that the compiler stops the build when those mismatches occur, so you will not get an .exe until the errors are fixed. The practical fixes are: make the matrix dimensions plain integer fields, make the matrix storage a well-defined pointer type, allocate every pointer before you use it, and be consistent about C vs C++ allocation rules.

A simple, safe pattern in C is to keep the struct tiny and allocate explicitly:

typedef struct {
    double **data;
    int rows;
    int cols;
} Matrix;

Matrix *create_matrix(int rows, int cols) {
    Matrix *m = malloc(sizeof *m);
    int i;
    if (!m) return NULL;
    m->rows = rows; m->cols = cols;
    m->data = malloc(rows * sizeof *m->data);
    if (!m->data) { free(m); return NULL; }
    for (i = 0; i < rows; ++i) {
        m->data[i] = malloc(cols * sizeof *m->data[i]);
        if (!m->data[i]) { /* free already allocated rows */ }
    }
    return m;
}

void free_matrix(Matrix *m) {
    int i;
    if (!m) return;
    for (i = 0; i < m->rows; ++i) free(m->data[i]);
    free(m->data); free(m);
}

Key notes: check every malloc return, free in the reverse order of allocation, never use an uninitialized pointer, and place getch() (or any pause) before return so it actually runs. If you are compiling as C++ (Dev-C++ default), either change the filename to .c or use C++ allocations (new/std::vector) or cast malloc results. Avoid typedefs that hide array sizes when you intend dynamic sizing — they make pointer types confusing.

Fix small pieces at a time: first get a tiny program that allocates and prints a single element, then expand to filling and freeing the whole matrix.

Recommended Answers

All 2 Replies

When there are compiler errors, you get no .exe. You need to fix the errors before the program will be created; once an .exe is build you can run it.

You're trying to compare an int to a structure:

//structure for dimension
typedef struct{
       mNum **pX;//declares double pointer for matrix
       mDim m,n; //variable size of matrix
}structDimension;

//structure for run-time initialization of matrix
mNum *initializeMatrix(structDimension n){
     mNum **temp;
     int ctr;
     for(ctr=0 ; ctr<n ; ctr++)

Color the compiler confused.

here's my new code:

i still get errors, please help me correct them..
i am not very familiar with correcting errors,, need help
i am getting confused with the structures [it is required to use structures for this project]

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
typedef double mNum[10];
typedef int *mDim;
 
//structure for dimension
typedef struct{
       mNum **pX;//declares double pointer for matrix
       mDim m,n; //variable size of matrix
}structDimension;
 
//structure for run-time initialization of matrix
mNum **initializeMatrix(structDimension size){
     mNum **temp;
     int ctr;
     for(ctr=0 ; ctr<size.n[ctr] ; ctr++)
{
          temp[ctr] = malloc(sizeof(mNum)*size.n[ctr]);
}
     return temp;
}
mDim *initializeRC(){
     mDim *a, *b;
     int ctr;
     a[ctr] = malloc(sizeof(mDim) * 10);
     b[ctr] = malloc(sizeof(mDim) * 10);
}
 
int main (int args, char *argv[]){
   structDimension **matrix, row, col;
    int ctr; //counter - initializing memory location
    int *ctrrow, *ctrcol;//counter for clearing matrix and displaying values
    int ans; 
 
    row.m[ctr] = 1;
    col.n[ctr] = 1;
    for(ctrrow=0 ; ctrrow<row.m ; ctrrow++){
          for(ctrcol=0 ; ctrcol<col.n ; ctrcol++){
                       matrix[ctr][ctrrow][ctrcol] = 0;
          }
    }
    printf("\n\nGood Day!\n\n");
    printf("HELLO!");
 
free(matrix);  
return 0;
getch ();
 
}

....cpp: In function `double (** initializeMatrix(structDimension))[10]':
....cpp:20: error: invalid conversion from `void*' to `double (*)[10]'

....cpp: In function `int** initializeRC()':
....cpp:27: error: invalid conversion from `void*' to `int*'
....cpp:28: error: invalid conversion from `void*' to `int*'
....cpp: In function `int main(int, char**)':
....cpp:41: error: invalid types `structDimension*[int*]' for array subscript

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.