0

I want to pass a pointer to a multidimensional Array, so the value could be kept not copied. How can I do it? I also keep tracking the int count, will it work every time? The array that I need is memory. The struct has been declared before, outside the main.

struct registers
{
   int data;     
} registerX, registerY;

void first(int *counter, struct registers* X1, int **m)
{
  int value;
  printf("Enter the value for the X\n");
  scanf("%d", &value);
  X1->data = value;
  m[*counter][1] = X1->data;
  *counter = *counter++;

}
int main()
{
 int memory[SIZE][2];
 int count = 0;
 int choice;
 printf("Enter the instruction number:\n");

 while(choice != 107)
{
  scanf("%d", &choice);
  if(choice == 101)
  {
        memory[count][0] = 101;
        first(&count, &registerX, &memory[count][1]);
  } 
5
  • &memory[count][1] is int *. receive int *m, then m[*counter][1] = X1->data; --> *m = X1->data;, *counter = *counter++; --> ++*counter; or *counter += 1; Commented Nov 29, 2014 at 13:40
  • What is the difference between *counter++ and ++*counter? Commented Nov 29, 2014 at 14:11
  • 1
    *counter++ meant *(counter++). ++ for increment pointer. ++*counter is increment *counter i.e. (*counter)++. Commented Nov 29, 2014 at 14:14
  • @BLUEPIXY except that ++*counter is not the same as (*counter)++. Commented Nov 29, 2014 at 17:11
  • @TheParamagneticCroissant produce the results of the *counter += 1. I do not say to be the same. Commented Nov 29, 2014 at 17:15

2 Answers 2

2

The function signature should be:

void first(int *counter, struct registers* X1, int m[][2])

Or equivalently:

void first(int *counter, struct registers* X1, int (*m)[2])

The call should be:

first(&count, &registerX, memory);
Sign up to request clarification or add additional context in comments.

4 Comments

For the first one, will the values be saved? Because I need to print them at the end from main.
@AbylIkhsanov: the first and second declarations are identical. They're just different ways of spelling the exact same thing. In both cases the function parameter is a pointer to the first element of the array (which is an array of arrays, so the first element is itself an array).
I have changed my code by using your first example, now the problem is that array does not store any values, it keeps giving 0, at the line where :X1->data = value; m[*counter][1] = X1->data;, X1->data stores the value but not the array, that is strange
@AbylIkhsanov: That is strange, and I'm inclined to believe that your problem lies elsewhere. Start over, make a minimal program that only exercises your array logic, and then build it up once you understand how each part works.
0
  • First of all, apart from what Kerrek SB said, you could replace

    *counter = *counter++;
    

    with this

    (*counter)++;
    

    EDIT: Sorry, I made a mistake trying to say what's wrong with *counter = *counter++, but I got some strange results with the sentence *pointer = *pointer++.

  • Second, I see that you are using registerX while it's just a type, so you could first do this.

    registerX *rgx = NULL;
    rgx = malloc(sizeof(registerX));
    

    and use.

    first(&count, rgx, memory);
    

Considering what I said above, this code worked for me.

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

#define SIZE 5

typedef struct registers {
    int data;     
} registerX, registerY;

void first(int *counter, struct registers *X1, int m[][2]) {
    int value;

    printf("Enter the value for the X: ");
    scanf("%d", &value);

    X1->data = value;
    m[*counter][1] = X1->data;

    (*counter)++;

    return ;
}

int main() {
    int memory[SIZE][2];
    int count = 0;
    int choice;

    registerX *rgx = NULL;
    rgx = malloc(sizeof(registerX));

    printf("Enter the instruction number: ");

    while(choice != 107) {
        scanf("%d", &choice);

        if (choice == 101) {
            memory[count][0] = 101;
            first(&count, rgx, memory);
            printf("Number %d was stored in memory[%d][%d]\n\n", memory[count-1][1], count-1, 1);
        }

        printf("Enter more instructions: ");
    }

    return 0;
}

Comments

Your Answer

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