2
#include <stdio.h>
#include <iostream>

using namespace std;

    int main(void)
    {
bool premiereLignefaite = false;
//Lire le fichier
FILE * graphe = fopen("graphe.txt", "r");
//Fichier de sortie
FILE * resultat = fopen("resultat.txt", "w");
int nbr1, nbr2;
int *matrice; //pointeur vers la matrice d'adjacence

//Ligne lue
static char ligne[50];

while (fgets(ligne, 50, graphe) != NULL) //retourne 0 quand on a end-of-file
{
    //La premiere ligne est différente
    if (premiereLignefaite == false) {
        //Initialiser une matrice d'adjacence NxN
        sscanf(ligne, "%d %d", &nbr1, &nbr2);
        matrice =  new int(nbr1 * nbr1); //Memoire dynamique pour la matrice dadjacence n x n
        premiereLignefaite = true;
        continue;
    }
    //On construit notre matrice d'adjacence
    sscanf(ligne, "%d %d", &nbr1, &nbr2);
    matrice[nbr1][nbr2] = 1;
}

int u = 2+2;


return 0;
 }

So I'm getting an error on this line : matrice[nbr1][nbr2] = 1; I'm just trying to build an adjacency list from a text file. I don't understand what I'm doing wrong. Thank you.

EDIT: Since people ask about it, this is my graph file. The first line is the number of vertices and the number of edges(not useful imo) The following lines are my edges, I use the first line to allocate memory for a NxN graph and the following lines to fill in my adjacency matrix.

9 20
0 1
0 2
1 0
1 2
1 3
1 5
2 0
2 1
2 3
3 1
3 2
3 4
4 3
5 1
5 6
5 7
6 5
6 8
7 5
8 6

3 Answers 3

2

int *matrice; means that matrice is a pointer to an int (or ints), so matrice[a] will give you an int. A pointer does not have any information about the dimensions of the array, so you can't do two-dimensional access.

You want to do store the dimensions of your array and then do

matrice[nbr1 * numberOfColumns + nbr2] = 1;

Side notes:

  • Raw array access via pointers can be VERY dangerous if you're not careful with bounds checking. Consider std::vector<>.
  • You probably meant new int[nbr1 * nbr2]?
Sign up to request clarification or add additional context in comments.

3 Comments

I don't need to add *sizeof(int) because C++ takes of it automatically right?
I got my matrix working with your technique. It's too bad that you can't use [] [] syntax for multi dimensional dynamic arrays! I really hoped I could do that.
Great to hear it worked! To answer your question - yes, new will automatically allocate sizeof(type) times the number of elements. You would have to use sizeof() if you were to use C's malloc().
2

matrice[x] is the same thing as *(matrice+x), and matrice[x][y] is the same thing as *(*(matrice+x)+y).

So the problem is that when you write matrice[nbr1][nbr2], that's the same as writing *(*(matrice+nbr1)+nbr2). Since matrice is only a pointer, and not a pointer to a pointer, this of course doesn't work.

1 Comment

Thanks for the unfold explanation!
1

matrice declared as int * which makes it a signle-dimensional array. It could not be accessed as multidimensional array matrice[nbr1][nbr2]. Also check your memory allocations code. It should be new int[nbr1 * nbr2], not new int(nbr1 * nbr2).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.