What I am trying to do is use a structure to create and display a 2D array of characters in the function 'function1( )'. This array will be sent back to the main( ) so I can use it further in my program. However my program is plagued with problems. I am having trouble with pointers. I assume my problem is somewhere with either my pointers or my variables. I've tried several combinations with no effort. As a beginner, it probably is some odd combination that is not coming to my mind.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 13
#define COL 16
typedef struct letter_array {
char** letters;
struct letter_array *ltr_ptr;
} larray;
void function1 (larray ** letter1[*][16]);
int function2 (larray letter2[][16]);
int function3 (larray letter3[][16]);
void function4 (int hor_ans, int ver_ans);
int main (void)
{
larray letter_list;
int vert, hori, **lptr;
lptr = malloc(ROW*sizeof(int*));
for(vert = 0; vert<ROW; vert++)
{
lptr [vert] = malloc(COL*sizeof(int));
}
printf("\n \t\t\t *** Hello! ***");
printf("\n This program will create a random selection of 180 upper-case"
" characters. \n\n");
function1(&letter_list); //Problem #1
printf("\n\nThank you for using my random character array program. \n"
"\t\t Have a good day! \n");
return ( 0 ) ;
}
void function1 (larray **letter1 [][16])
{
int i, z, funptr;
srandom((unsigned)time(NULL));
for(i=0; i<12; i++)
{
letter1 [i] <- (int*) funptr; // Problem #2-3
for(z=0; z<15; z++)
{
letter1[i][z] = random( )%26+'A'; // Problem #4
printf("%c ", letter1[i][z]);
}
printf("\n");
}
return ;
}
The errors are below and commented.
warning:passing argument 1 of 'function1' from incompatible pointer type
warning: cast to pointer from integer of different size
error: wrong type argument to unary minus
warning: assignment makes pointer from integer without a cast
I hope this helps.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 13
#define COL 16
typedef struct letter_array {
char** letters;
struct letter_array *ltr_ptr;
} larray;
void function1 (larray * letter1); // here you just need a pointer to the structure
int function2 (larray letter2[][16]);
int function3 (larray letter3[][16]);
void function4 (int hor_ans, int ver_ans);
int main (void)
{
larray letter_list;
int vert, hori;
letter_list.letters = malloc(ROW*sizeof(int*)); // allocate memory to the char pointer in the structure
for(vert = 0; vert<ROW; vert++)
{
letter_list.letters[vert] = malloc(COL*sizeof(int)); // allocate the second 2D
}
printf("\n \t\t\t *** Hello! ***");
printf("\n This program will create a random selection of 180 upper-case"
" characters. \n\n");
function1(&letter_list); //Problem #1 pass a pointer to the structure
printf("\n\nThank you for using my random character array program. \n"
"\t\t Have a good day! \n");
return ( 0 ) ;
}
void function1 (larray *letter1) // just needs a pointer to the structure
{
int i, z;
srandom((unsigned)time(NULL));
for(i=0; i<ROW; i++) // used ROW
{
//letter1->letters[i] <- (int*) funptr; // Problem #2-3 this line not needed as near as i can tell
for(z=0; z<COL; z++) // used COL
{
letter1->letters[i][z] = random( )%26+'A'; // Problem #4 dereference pointer to member char **
printf("%c ", letter1->letters[i][z]);
}
printf("\n");
}
return ;
}
Related
so I'm trying to make a 2d binary matrix that is the size provided by stdin and that has randomly assigned indexes for the 0 and 1, however, their cannot be more than size/2 zeroes or ones.
For example
an input of 2
could output
1 0
0 1
Now I was going to originally just use the argument int arr[][n] in init but this idea failed since passing in the matrix just resulted in my program going on some sort of an infinite loop when I attempted to access matrix again inside of the main function. I believe this happened because the lifespan of matrix expired when init concluded? So my question here is why is what I'm doing now producing the below error and how can I fix this up?
note: expected ‘int * (*)[(sizetype)(n)]’ but argument is of type ‘int (*)[(sizetype)(dim)][(sizetype)(dim)]’
7 | int init(int n, int* arr[][n]);
My code
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int init(int n, int* arr[][n]);
int main(){
time_t t;
srand((unsigned) time(&t));
int dim;
printf("Enter a positive even integer: ");
scanf("%d",&dim);
if(dim<2||dim>80){
return 1;
}
int matrix[dim][dim];
init(dim,&matrix);
return 0;
}
int init(int n, int* arr[][n]){
int numZeroes,numOnes;
int zero_or_one;
for(int row=0;row<n;row++){
numZeroes=0;
numOnes=0;
for(int col=0;col<n;col++){
if(numZeroes<n/2 && numOnes<n/2){
zero_or_one=rand()%2;
*arr[row][col]=zero_or_one;
if(zero_or_one==1){
numOnes++;
}
if(zero_or_one==0){
numZeroes++;
}
}
else{
if(numZeroes==n/2 && numOnes<n/2){
*arr[row][col]=1;
}
if(numZeroes<n/2 && numOnes==n/2){
*arr[row][col]=0;
}
}
}
}
for(int row=0;row<n;row++){
for(int col=0;col<n;col++){
printf("%d ",*arr[row][col]);
}
printf("\n");
}
return 0;
}
The function should be declared like
void init(int n, int arr[][n]);
(the return type int of the function does not make a great sense.)
and called like
init(dim, matrix);
Within the function instead of statements like this
*arr[row][col]=zero_or_one;
you have to write
arr[row][col]=zero_or_one;
I have this exercise to make a transpose of a matrix in C. I made a function to check for type n*n but when I'm trying to ask the user for the matrix I don't know how I should declare the array. And I'm getting this compile error "type of formal parameter 1 is incomplete" in the function on the [n2] part.
The parameters of the functions for multi dimensional arrays shouldn't be like this -> int matrix[][n2]. or is cause i'm using a variable and not a constant or a pre defined size. ?
#include <stdio.h>
#define prompt "Dimenção da matriz (nxn) >>"
#define prompt_1 "Introduza os valores : "
void getType( int *n1, int *n2 );
void getMatrix( int matrix[][n2], int lim1, int lim2);
//void trans(int matrix[][n2]);
int main(int argc, char const *argv[]) {
int n1, n2;
getType(&n1, &n2);
int matrix[n1][n2];
//printf("%dx%d\n", n1, n2);
getMatrix(matrix, n1, n2);
//trans(matrix);
return 0;
}
void getType(int *n1, int *n2){
printf("%s", prompt );
scanf("%dx%d", &(*n1), &(*n2));
}
void getMatrix( int matrix[][n2], int lim1, int lim2){
printf("%s\n", prompt_1 );
for(int line = 0; line < lim1; line++ ){
for(int column = 0; column < lim2; column++){
printf("Linha %d coluna %d ->", line, column );
scanf("%d", &matrix[line][column]);
}
}
}
The signature should be:
void getMatrix( int lim1, int lim2, int matrix[lim1][lim2] )
You are allowed omit the lim1 inside square brackets but it is good documentation to include it.
The main point is that the variable inside the square brackets must either be a parameter from earlier in the parameter list, or some other variable in scope (which can only be a global variable, but that's usually a bad idea).
Also it would be good to check scanf return value otherwise you may create matrix with garbage dimension.
I was having some difficulty in finding and solving a problem with the dreaded segmentation error. I have created a "struct" with an array and filled with with random characters. From there I am counting the horizontal and vertical pairs. Everything seems fine until I run function3(). From there comes the segmentation fault. I ran GDB to find the error, but I do not know why it doesn't work since I have done a similar function for function2() and it is okay with that function. I'm not sure if I am missing a pointer or not. I've played around with adding and subtracting pointers with no luck.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 12
#define COL 15
typedef struct letter_array {
char** letters;
struct letter_array *ltr_ptr;
} larray;
void function1 (larray * letter1);
int function2 (larray * letter2);
int function3 (larray * letter3);
void function4 (int hor_ans, int ver_ans);
int main ( void )
{
larray letter_list;
int vert, hori, count;
letter_list.letters = malloc(ROW*sizeof(int*));
for(count = 0; count<ROW; count++)
{
letter_list.letters [count] = malloc(COL*sizeof(int));
}
printf("\n \t\t\t *** Hello! ***");
printf("\n This program will create a random selection of 180 upper-case"
" characters. \n\n");
function1(&letter_list);
hori = function2(&letter_list);
vert = function3(&letter_list); //The Problem?
free(letter_list.letters);
return ( 0 ) ;
}
void function1 (larray *letter1) // Assign random letters to array.
{
int i, z;
srandom((unsigned)time(NULL));
for(i=0; i<ROW; i++)
{
for(z=0; z<COL; z++)
{
letter1->letters[i][z] = random( )%26+'A';
printf("%c ", letter1->letters[i][z]);
}
printf("\n");
}
return ;
}
int function2 (larray * letter2) //Count horizontal pairs.
{
int a,b;
int m=0;
for(a=0; a<ROW; a++)
{
for(b=0; b<COL; b++)
{
if (letter2->letters[a][b] == (letter2->letters[a][b+1]))
m++;
}
}
return (m);
}
int function3 (larray * letter3) //Count vertical pairs.
{
int a,b;
int n=0;
for (a=0; a<ROW; a++)
{
for(b=0; b<COL; b++)
{
if (letter3->letters[a][b] == (letter3->letters[a+1][b])) //THE Problem..?
n++;
}
}
return (n);
In GDB...
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400ad8 in function3 (letter3=0x7fffffffd8a0)
xxx if (letter3->letters[a][b] == (letter3->letters[a+1][b]))
(gdb) backtrace
#0 0x0000000000400ad8 in function3 (letter3=0x7fffffffd8a0)
#1 0x000000000040088f in main ()
(gdb) up
#1 0x000000000040088f in main ()
xxx vert = function3(&letter_list);
Thank you for your help!
It's pretty obvious. GDB tells you exactly where to look. In your function3 you do
for (a=0; a<ROW; a++)
and then you try to access
letter3->letters[a+1][b]
here, a+1 causes the segmentation fault (you run off the edge of your array).
I have been struggling for a while with my program. I am trying to find horizontal pairs in an array that is setup in function1. My goal is to change the original array in another function. Then process that array to find a horizontal pair.
One problem that has occurred is when I run the program, the result is zero. Another problem is the gcc warning in function 1, warning: comparison between pointer and integer. The other problem is another gcc warning (marked by the **) warning: passing argument 1 of 'function1' from incompatible pointer type.
I appreciate any help, as a beginner, I have spent several hours on this problem and have tried to find solutions, but trying to use pointers and using struct and typedef have not worked. :(
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void function1 (int letter1[][16]);
void function2 (int letter2[][16]);
int main ( void )
{
int letter_array [13][16];
printf("\n \t\t Hello!");
printf("\n This program will create a random selection of 180 upper-case"
" characters. \n");
**function1(&letter_array);**
function2(letter_array);
return ( 0 ) ;
}
void function1 (int letter1 [][16])
{
int i, z;
srandom((unsigned)time(NULL));
for(i=0; i<=11; i++)
{
for(z=0; z<=14; z++)
{
letter1 [i][z] = random( )%26+65;
printf("%c ", letter1 [i][z]);
}
printf("\n");
}
return ;
}
void function2 (int letter2 [][16])
{
int a,b;
int m=0;
for( a = 0; a <= 11; a++)
{
for( b = 0 ; b <= 14; b++)
{
if (letter2 == (letter2 + 1))
m++;
}
}
printf("\nThe number of horizontal pairs of characters"
" are: %d", m);
return ;
Just remove the ampersand & from the argument.
Change
function1(&letter_array);
to
function1(letter_array);
EDIT:
Also change
if (letter2 == (letter2 + 1))
to
if (letter2[a][b] == (letter2[a][b+1]))
The warning is occurring because you are passing a pointer to an array[][16] into function1 instead of the array itself. This can be resolved by removing the &:
function1(letter_array);
The program is returning 0 because of the return statement at the end of your main function.
I'm working on this quite simple C program, wich builds an histogram from an array. What it does in particular, is writing on a second array the ciphres of the first array, followed by the number of their occurences. For instance: if the array A is {2,3,2,5,6,6,6} the array histogram of A, will be {2,2,3,1,5,1,6,3}. Ok, so my program compiles well and there aren't any warnings or errors. But the program stops working after i' ve inserted the values of array A. Where am i failing??? Thank you!!
typedef unsigned short int boolean;
#define TRUE 1
#define FALSE 0
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <conio.h>
boolean Array_Histogram(int *A, int N, int **H, int *count){
int i,j;
boolean found;
*H = (int *) malloc( sizeof(int)*2*N );
if(*H==NULL)
return FALSE;
(*count)=0;
for(i=0;i<N;i++){
found=FALSE;
j=0;
while(found==FALSE && j<(*count)*2){
if(A[i]==(*H[j]))
found=TRUE;
else j+=2;
}
if(found==TRUE){
(*H)[j+1]++;
}
else{
(*H)[j] = A[i];
(*H)[j+1] = 1;
(*count) ++;
}
}
return TRUE;
}
int main(){
int N;
int count;
int *A;
int **H;
int *i;
i=0;
printf("Inserisci N, dimensione dell'array A:");
scanf("%d", &N);
if(N<=0){
return 0;
}
A= (int*) malloc (sizeof(int)*N);
*H = (int *) malloc( sizeof(int)*2*N );
for(count=0;count<N;count++){
printf("\n Inserisci il valore %d di A:", count);
scanf("%d", &A[count]);
}
Array_Histogram(A,N,H,i);
printf("\nI valori dell'istogramma sono:");
for(count=0;count<2*N;count++)
printf("\n %d", (*H)[count]);
return 0;
}
The variable H in main is of the wrong type. It should be declared:
int *H;
You need to change 2 more lines in main with regards to this:
H = malloc( sizeof(int)*2*N );
and
Array_Histogram(A,N,&H,i);
Also, you need to allocate H only once.
I am not really sure what are you trying to achieve but your problem is de-referencing null pointer.
H is double pointer to integer. so you have to allocate memory to H using malloc before using *H
int **H;
*H = malloc( sizeof(int)*2*N );
Edit:
Suggest you to read some basics about pointer here
You are allocating memory for H twice(once in main, once in the func).
H is declared as int **, memory is being allocated to 2*N elements of integer pointers each, which is probably not what you wanted to do.
If you just want output as {2,2, 3,1..}, then probably you just want a int *H;