‘matrix’ undeclared (first use in this function)

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1192 Views - Last Post: 14 February 2012 - 03:47 PM Rate Topic: -----

#1 hsaunds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 16-October 11

‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 12:26 PM

Why is it complaining about matrix and not key? I am still not comfortable with pointers in C so I image that is where my issues are coming from..

char **encrypt(int key, char** matrix){

int i, j;

        for(i=0; i<50; i++){
        for(j=0; j<50; j++){
                if(matrix[i][j]>='a'|| matrix[i][j]<='z'){
                        matrix[i][j]=matrix[i][j]+key;
                        }
                } 
        }
return matrix;
}





Is This A Good Question/Topic? 0
  • +

Replies To: ‘matrix’ undeclared (first use in this function)

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 12:43 PM

Could you post the smallest possible program to show us your problem? If you could also post the exact Error/Warning message you are receiving, that could help too.

We don't have enough of a "context" to be able to tell you 100% what your problem is. If you show us where you call this function and what you call it with, it could probably help us help you.

This post has been edited by vividexstance: 14 February 2012 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 hsaunds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 16-October 11

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 12:58 PM

here is my code so far. I'm not done with the exact encryption algorithm but I just want to test what I have so far.
The errors I am getting are:

cipher.c: In function ‘main’:
cipher.c:89: warning: passing argument 1 of ‘printf’ from incompatible pointer type
/usr/include/stdio.h:339: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’
cipher.c:89: warning: format not a string literal and no format arguments
cipher.c:92: error: ‘matrix’ undeclared (first use in this function)
cipher.c:92: error: (Each undeclared identifier is reported only once
cipher.c:92: error: for each function it appears in.)

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

char** input();
char** encrypt(int key,char** matrix);

char** input(){
/*initialize empty matrix*/

int i,j,k,l;;
char** matrix;
char* text;

for(i=0;i <50; i++)
        {
                for (j=0; j<50; j++){
                        matrix[i][j]=' ';
                }
        }
/*create matrix from user input*/

printf("Enter text\n");

        while(fgets(text,sizeof(text),stdin) != NULL)
        {
                for(k=0; k<50; k++){
                        if(text[k] ==' '){
                                k++;
                        }
                        else if(text[k] == '\n'){
                                break;
                        }
                        else{
                                matrix[0][k] = text[k];
                        }
                }
        }
        return matrix;
}

char** encrypt(int key, char** matrix){

int i, j;

        for(i=0; i<50; i++){
        for(j=0; j<50; j++){
                if(matrix[i][j]>='a'|| matrix[i][j]<='z'){
                        matrix[i][j]=matrix[i][j]+key;
                        }
                }
        }
return matrix;
}


void displayMenu(){

printf("MAIN MENU \n========\n");
printf("1. Input Text\n");
printf("2. Encrypt\n");
printf("3. Decrypt\n");
printf("4. Exit\n");
printf("Selection:__\n");

}
void main(){

displayMenu();

int choice;

scanf("%d", &choice);

if(choice==1){
input;
}
if(choice==2){
        int key;
        char** matrix;

        printf("Please enter your key\n");
        scanf("%d", &key);

        matrix = input();
        printf(encrypt(key, matrix));
}
if(choice==4){
exit(0);
}
}




Was This Post Helpful? 0
  • +
  • -

#4 hsaunds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 16-October 11

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 01:05 PM

caught some of my silly errors already. forgot to format the printf statement and got rid of my first random matrix declaration in main!
Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 01:16 PM

Here's a tip, for C/C++ the main function is defined as a function that returns an "int" not void.

What is the encrypt function supposed to return? An encrypted string? An encrypted string of strings? By declaring the return value as a "char**", the compiler expects the function to return a pointer to a pointer of chars. If the encrypt function is just supposed to return a single string, then you need to change the function definition to just return a "char*". That is why you are receiving the warning about converting the first argument of printf from char** to char*.

Your main function seems a little off. Can you tell me what you think this bit of code does?
if(choice==1){
input;
}

Was This Post Helpful? 0
  • +
  • -

#6 hsaunds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 16-October 11

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 01:52 PM

hmm alright I fixed a bit more but am stuck with this function at the moment. My plan is for input() to return a 2d array, and for encrypt() to use input as its argument and also return a 2d array.
I want input to take in user input using fgets, and create a 2d array of char from each line that the user inputs. Buuuut when I call this function I get a segmentation fault??

char** input(){
/*initialize empty matrix*/

int k,l;
char** matrix;
char* text;

matrix = malloc(2500);

/*create matrix from user input*/

printf("Enter text\n");

        while(fgets(text,sizeof(text),stdin) != NULL)
        {
                for(k=0; k<50; k++){
                        if(text[k] == '\n'){
                        k++;
                        }
                for(l=0;l<50; l++){
                        matrix[k][l]=text[k];
                        }
                        }
                }
        return matrix;
}


Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 01:52 PM

Where are you allocating any memory for your **matrix and *test? You seem to be trying to use these variables before you allocated any memory to them.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 hsaunds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 16-October 11

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 02:00 PM

am I not allocating it using malloc?


char** input(){
/*initialize empty matrix*/

int k,l;
char** matrix;
char* text;

matrix = malloc(2500);/*create matrix from user input*/
text = malloc(50);

printf("Enter text\n");

        while(fgets(text,sizeof(text),stdin) != NULL)
        {
                for(k=0; k<50; k++){
                        if(text[k] == '\n'){
                        k++;
                        }
                for(l=0;l<50; l++){
                        matrix[k][l]=text[k];
                        }
                        }
                }
        return matrix;
}


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 02:04 PM

Quote

am I not allocating it using malloc?

Maybe now, but not until the last post.

Post your current code, plus any error/warning messages, exactly as they appear in your development environment, and a current problem description.

Jim

This post has been edited by jimblumberg: 14 February 2012 - 02:05 PM

Was This Post Helpful? 0
  • +
  • -

#10 hsaunds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 16-October 11

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 02:13 PM

I declared the array dimensions instead. Now I am getting this:

cipher.c: In function ‘input’:
cipher.c:29: warning: return from incompatible pointer type
cipher.c:29: warning: function returns address of local variable



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

char** input();
char** encrypt(int key,char** matrix);

char** input(){
/*initialize empty matrix*/

int k,l;
char matrix[50][50];
char text[50];

printf("Enter text\n");

        while(fgets(text,sizeof(text),stdin) != NULL)
        {
                for(k=0; k<50; k++){
                        if(text[k] == '\n'){
                        k++;
                        }
                for(l=0;l<50; l++){
                        matrix[k][l]=text[k];
                        }
                        }
                }
        return matrix;
}


char** encrypt(int key, char** matrix){

int i, j;

        for(i=0; i<50; i++){
        for(j=0; j<50; j++){
                if(matrix[i][j]>='a'|| matrix[i][j]<='z'){
                        matrix[i][j]=matrix[i][j]+key;
                        }
                }
        }
return matrix;
}
void displayMenu(){

printf("MAIN MENU \n========\n");
printf("1. Input Text\n");
printf("2. Encrypt\n");
printf("3. Decrypt\n");
printf("4. Exit\n");
printf("Selection:__\n");

}
int main(){

displayMenu();

int choice;

scanf("%d", &choice);

if(choice==1){
input();
}
if(choice==2){
        int i,j,key;

        printf("Please enter your key\n");
        scanf("%d", &key);

        char** matrix = input();
        for(i=0; i<50; i++){
                for(j=0; j<50; j++){
        printf("%c", (encrypt(key, matrix))[i][j]);
}
}
}
if(choice==4){
exit(0);
}
}



Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 02:20 PM

In the following snippet:
char** input() {
   /*initialize empty matrix*/

   int k,l;
   char matrix[50][50];
   char text[50];

   printf("Enter text\n");

   while(fgets(text,sizeof(text),stdin) != NULL)
   {
      for(k=0; k<50; k++) {
         if(text[k] == '\n') {
            k++;
         }
         for(l=0; l<50; l++) {
            matrix[k][l]=text[k];
         }
      }
   }
   return matrix;
}


You are trying to return a local variable, that will not work. I recommend you create the arrays in main and then pass the array to your functions as a parameter. Then you will not need to try to return that array in the return statement. You may want to read the function tutorials linked below in my signature.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 hsaunds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 16-October 11

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 02:51 PM

Alright, I've moved my input into my main. But I am still getting a segmentation fault right when the user inputs "1"??



int main(){

int choice, key, k, l, test;
char** matrix;
char* text;
matrix = malloc(2500);
text = malloc(50);

displayMenu();

scanf("%d", &key);

if(key==1){
test=1;
printf("Enter text\n");

        while(fgets(text,sizeof(text),stdin) != NULL)
        {
                for(k=0; k<50; k++){
                        if(text[k] == '\n'){
                        k++;
                        }
                for(l=0;l<50; l++){
                        matrix[k][l]=text[k];
                        }
                }
        }
}

if(choice==2){
        if(test!=1){
        printf("please enter characters first!");
 }
        int i,j,key;

        printf("Please enter your key\n");
        scanf("%d", &key);

        for(i=0; i<50; i++){
                for(j=0; j<50; j++){
        printf("%c/n", (encrypt(key, matrix)[i][j]));
}
}


Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 03:04 PM

What header files are you including?

Jim
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 03:04 PM

On line 30, you are checking to see if the value of 'choice' is equal to '2', but at that point you haven't initialized it yet. Always remember to initialize you're variables before you use them because they might contain garbage values. You can usually initialize a variable when you define it:
int key = 0;
char* str = malloc(sizeof(char) * 80);


*NOTE*: When you dynamically allocate memory by using 'malloc', somewhere later on in the program(usually at the end of main) you must deallocate that memory using the 'free' function. If you don't free the memory then your program has memory leaks. On most modern systems, this isn't that big of an issue but if you want to learn how to program correctly, you must deallocate any memory that you dynamically allocate!
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: ‘matrix’ undeclared (first use in this function)

Posted 14 February 2012 - 03:21 PM

Also if you run your program through your debugger it should tell you the line where the problem is being detected. Then you can look at the variables in question and see if any look wrong.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2