C programming problem pointers and arrays 2d
up vote
1
down vote
favorite
Q2: Implelment the follwoing function ArrPrintMatrix(char *(p)[7])
which prints the content of the matrix[m][7] ={"SHAHBAZ","AYAZ"}
in to 3x3 Matrix
Sample Output
S H A
H B A
Z A Y ..
My question is : here is code only problem i am getting is a space after one name is completed..how to remove that space . I have this question in my assignments , that have to be submitted on sunday (11-11-18)..
My code is:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i;
for(i=0;i<16;i++)
{
if(i>=9)//since 3 by 3 matrix is required
break;
if(i==3||i==6||i==9)//changing line since 3 by 3 matrix is needed
printf("n");
printf("%c ",*(p+i));//prininting chracters
}
}
c function pointers multidimensional-array
add a comment |
up vote
1
down vote
favorite
Q2: Implelment the follwoing function ArrPrintMatrix(char *(p)[7])
which prints the content of the matrix[m][7] ={"SHAHBAZ","AYAZ"}
in to 3x3 Matrix
Sample Output
S H A
H B A
Z A Y ..
My question is : here is code only problem i am getting is a space after one name is completed..how to remove that space . I have this question in my assignments , that have to be submitted on sunday (11-11-18)..
My code is:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i;
for(i=0;i<16;i++)
{
if(i>=9)//since 3 by 3 matrix is required
break;
if(i==3||i==6||i==9)//changing line since 3 by 3 matrix is needed
printf("n");
printf("%c ",*(p+i));//prininting chracters
}
}
c function pointers multidimensional-array
Nasty question. It tells you to write a function like thisArrPrintMatrix(char *(p)[7])
and gives you a string that's 8 characters long ("SHAHBAZ" plus a terminating zero).
– Tim Randall
Nov 9 at 16:42
thank you so much for quick reply . I donot understand hot to useArrPrintMatrix(char *(p)[7])
and to pass it to function can you please please help me ....please
– Âftãb Bãlôçh
Nov 9 at 16:46
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Q2: Implelment the follwoing function ArrPrintMatrix(char *(p)[7])
which prints the content of the matrix[m][7] ={"SHAHBAZ","AYAZ"}
in to 3x3 Matrix
Sample Output
S H A
H B A
Z A Y ..
My question is : here is code only problem i am getting is a space after one name is completed..how to remove that space . I have this question in my assignments , that have to be submitted on sunday (11-11-18)..
My code is:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i;
for(i=0;i<16;i++)
{
if(i>=9)//since 3 by 3 matrix is required
break;
if(i==3||i==6||i==9)//changing line since 3 by 3 matrix is needed
printf("n");
printf("%c ",*(p+i));//prininting chracters
}
}
c function pointers multidimensional-array
Q2: Implelment the follwoing function ArrPrintMatrix(char *(p)[7])
which prints the content of the matrix[m][7] ={"SHAHBAZ","AYAZ"}
in to 3x3 Matrix
Sample Output
S H A
H B A
Z A Y ..
My question is : here is code only problem i am getting is a space after one name is completed..how to remove that space . I have this question in my assignments , that have to be submitted on sunday (11-11-18)..
My code is:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i;
for(i=0;i<16;i++)
{
if(i>=9)//since 3 by 3 matrix is required
break;
if(i==3||i==6||i==9)//changing line since 3 by 3 matrix is needed
printf("n");
printf("%c ",*(p+i));//prininting chracters
}
}
c function pointers multidimensional-array
c function pointers multidimensional-array
edited Nov 9 at 16:30
Fiddling Bits
7,11021938
7,11021938
asked Nov 9 at 16:28
Âftãb Bãlôçh
47
47
Nasty question. It tells you to write a function like thisArrPrintMatrix(char *(p)[7])
and gives you a string that's 8 characters long ("SHAHBAZ" plus a terminating zero).
– Tim Randall
Nov 9 at 16:42
thank you so much for quick reply . I donot understand hot to useArrPrintMatrix(char *(p)[7])
and to pass it to function can you please please help me ....please
– Âftãb Bãlôçh
Nov 9 at 16:46
add a comment |
Nasty question. It tells you to write a function like thisArrPrintMatrix(char *(p)[7])
and gives you a string that's 8 characters long ("SHAHBAZ" plus a terminating zero).
– Tim Randall
Nov 9 at 16:42
thank you so much for quick reply . I donot understand hot to useArrPrintMatrix(char *(p)[7])
and to pass it to function can you please please help me ....please
– Âftãb Bãlôçh
Nov 9 at 16:46
Nasty question. It tells you to write a function like this
ArrPrintMatrix(char *(p)[7])
and gives you a string that's 8 characters long ("SHAHBAZ" plus a terminating zero).– Tim Randall
Nov 9 at 16:42
Nasty question. It tells you to write a function like this
ArrPrintMatrix(char *(p)[7])
and gives you a string that's 8 characters long ("SHAHBAZ" plus a terminating zero).– Tim Randall
Nov 9 at 16:42
thank you so much for quick reply . I donot understand hot to use
ArrPrintMatrix(char *(p)[7])
and to pass it to function can you please please help me ....please– Âftãb Bãlôçh
Nov 9 at 16:46
thank you so much for quick reply . I donot understand hot to use
ArrPrintMatrix(char *(p)[7])
and to pass it to function can you please please help me ....please– Âftãb Bãlôçh
Nov 9 at 16:46
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
As a follow-on to my other answer, if you do use my logic to skip over the ''
that terminates the strings, you will need to use a different variable to keep track of how many characters you've actually printed, and just let i
keep track of where you are in the input string(s). Like so:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i, j;
for(i=0, j=0;i<16;i++)
{
if(j>=9)//since 3 by 3 matrix is required
break;
if(j==3||j==6||j==9)//changing line since 3 by 3 matrix is needed
printf("n");
if (*(p+i)==0) continue; //don't try to print the trailing ''
printf("%c ",*(p+i));//prininting chracters
j++; //increment counter of characters actually printed
}
}
Output:
S H A
H B A
Z A Y
Note the use of the j
variable, and how it is incremented with j++
only after actually printing a character.
if (*(p+i)==0) continue; //don't try to print the trailing ''
I donot understand this point , i mean why donot i say thay if *(p+j)=='0'??
– Âftãb Bãlôçh
Nov 9 at 17:06
'0'
is very different from0
;''
on the other hand equates to0
– landru27
Nov 9 at 17:13
you could use eitherif (*(p+i)==0)
orif (*(p+i)=='')
, but notif *(p+j)=='0'
, which is what you have in your comment
– landru27
Nov 9 at 17:15
ok got it thank you
– Âftãb Bãlôçh
Nov 9 at 17:20
can you help me understand this code please your explanation is tremendous#include<stdio.h> int main() { void ArrPrintMatrix(char (*p)[8]);//function declaration char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation ArrPrintMatrix(matrix);//calling function with base address } void ArrPrintMatrix(char (*p)[8]) { int i = 0, j = 0, k = 0; while (k != 9) { if (p[i][j] == '') { ++i; j = 0; } printf("%c ", p[i][j++]); ++k; if(k%3 == 0) printf("n"); } }
– Âftãb Bãlôçh
Nov 9 at 17:21
|
show 10 more comments
up vote
1
down vote
You should use char (*p)[8]
not char* p
The following code
could wrok:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char (*p)[8]);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(matrix);//calling function with base address
}
void ArrPrintMatrix(char (*p)[8])
{
// i will point to one of the strings in the set of strings
// j will point into the string we are inspecting
// k will count how many characters we have printed
int i = 0, j = 0, k = 0;
// we only need to print the first 9 printable characters we find
while (k != 9)
{
// if we have reached the end of an input string (the null-terminator),
// then move on to the next element in the array, and reset
// the string pointer to the beginning of the new string
if (p[i][j] == '') {
++i;
j = 0;
}
// print the character we are now pointing at,
// and increment the string pointer
printf("%c ", p[i][j++]);
// keep count of how many characters we have printed
++k;
// if k is divisible by 3, start a new row
if(k%3 == 0)
printf("n");
}
}
perfectly done i must appreciate bou unfortunately i don't understand how this code is working ..I also have exams on monday :(
– Âftãb Bãlôçh
Nov 9 at 16:59
can you please tech me your code by commenting or something like dry run..I would be thankfull
– Âftãb Bãlôçh
Nov 9 at 17:39
add a comment |
up vote
0
down vote
What you are missing is that there is a trailing ''
at the end of SHAHBAZ
, which you are also "printing", but because ''
does not have a character representation, you are seeing what looks like an 'extra' space.
Here is the smallest change I can think of to address this exact problem; add:
if (*(p+i)==0) continue; //don't try to print the trailing ''
just above your existing line:
printf("%c ",*(p+i));//prininting chracters
Output:
S H A
H B A
Z A
There are other things I would do differently than how you are doing them, but this addresses your exact question, using your coding style.
oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
– Âftãb Bãlôçh
Nov 9 at 16:53
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
As a follow-on to my other answer, if you do use my logic to skip over the ''
that terminates the strings, you will need to use a different variable to keep track of how many characters you've actually printed, and just let i
keep track of where you are in the input string(s). Like so:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i, j;
for(i=0, j=0;i<16;i++)
{
if(j>=9)//since 3 by 3 matrix is required
break;
if(j==3||j==6||j==9)//changing line since 3 by 3 matrix is needed
printf("n");
if (*(p+i)==0) continue; //don't try to print the trailing ''
printf("%c ",*(p+i));//prininting chracters
j++; //increment counter of characters actually printed
}
}
Output:
S H A
H B A
Z A Y
Note the use of the j
variable, and how it is incremented with j++
only after actually printing a character.
if (*(p+i)==0) continue; //don't try to print the trailing ''
I donot understand this point , i mean why donot i say thay if *(p+j)=='0'??
– Âftãb Bãlôçh
Nov 9 at 17:06
'0'
is very different from0
;''
on the other hand equates to0
– landru27
Nov 9 at 17:13
you could use eitherif (*(p+i)==0)
orif (*(p+i)=='')
, but notif *(p+j)=='0'
, which is what you have in your comment
– landru27
Nov 9 at 17:15
ok got it thank you
– Âftãb Bãlôçh
Nov 9 at 17:20
can you help me understand this code please your explanation is tremendous#include<stdio.h> int main() { void ArrPrintMatrix(char (*p)[8]);//function declaration char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation ArrPrintMatrix(matrix);//calling function with base address } void ArrPrintMatrix(char (*p)[8]) { int i = 0, j = 0, k = 0; while (k != 9) { if (p[i][j] == '') { ++i; j = 0; } printf("%c ", p[i][j++]); ++k; if(k%3 == 0) printf("n"); } }
– Âftãb Bãlôçh
Nov 9 at 17:21
|
show 10 more comments
up vote
0
down vote
accepted
As a follow-on to my other answer, if you do use my logic to skip over the ''
that terminates the strings, you will need to use a different variable to keep track of how many characters you've actually printed, and just let i
keep track of where you are in the input string(s). Like so:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i, j;
for(i=0, j=0;i<16;i++)
{
if(j>=9)//since 3 by 3 matrix is required
break;
if(j==3||j==6||j==9)//changing line since 3 by 3 matrix is needed
printf("n");
if (*(p+i)==0) continue; //don't try to print the trailing ''
printf("%c ",*(p+i));//prininting chracters
j++; //increment counter of characters actually printed
}
}
Output:
S H A
H B A
Z A Y
Note the use of the j
variable, and how it is incremented with j++
only after actually printing a character.
if (*(p+i)==0) continue; //don't try to print the trailing ''
I donot understand this point , i mean why donot i say thay if *(p+j)=='0'??
– Âftãb Bãlôçh
Nov 9 at 17:06
'0'
is very different from0
;''
on the other hand equates to0
– landru27
Nov 9 at 17:13
you could use eitherif (*(p+i)==0)
orif (*(p+i)=='')
, but notif *(p+j)=='0'
, which is what you have in your comment
– landru27
Nov 9 at 17:15
ok got it thank you
– Âftãb Bãlôçh
Nov 9 at 17:20
can you help me understand this code please your explanation is tremendous#include<stdio.h> int main() { void ArrPrintMatrix(char (*p)[8]);//function declaration char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation ArrPrintMatrix(matrix);//calling function with base address } void ArrPrintMatrix(char (*p)[8]) { int i = 0, j = 0, k = 0; while (k != 9) { if (p[i][j] == '') { ++i; j = 0; } printf("%c ", p[i][j++]); ++k; if(k%3 == 0) printf("n"); } }
– Âftãb Bãlôçh
Nov 9 at 17:21
|
show 10 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
As a follow-on to my other answer, if you do use my logic to skip over the ''
that terminates the strings, you will need to use a different variable to keep track of how many characters you've actually printed, and just let i
keep track of where you are in the input string(s). Like so:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i, j;
for(i=0, j=0;i<16;i++)
{
if(j>=9)//since 3 by 3 matrix is required
break;
if(j==3||j==6||j==9)//changing line since 3 by 3 matrix is needed
printf("n");
if (*(p+i)==0) continue; //don't try to print the trailing ''
printf("%c ",*(p+i));//prininting chracters
j++; //increment counter of characters actually printed
}
}
Output:
S H A
H B A
Z A Y
Note the use of the j
variable, and how it is incremented with j++
only after actually printing a character.
As a follow-on to my other answer, if you do use my logic to skip over the ''
that terminates the strings, you will need to use a different variable to keep track of how many characters you've actually printed, and just let i
keep track of where you are in the input string(s). Like so:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address
}
void ArrPrintMatrix(char *p)
{
int i, j;
for(i=0, j=0;i<16;i++)
{
if(j>=9)//since 3 by 3 matrix is required
break;
if(j==3||j==6||j==9)//changing line since 3 by 3 matrix is needed
printf("n");
if (*(p+i)==0) continue; //don't try to print the trailing ''
printf("%c ",*(p+i));//prininting chracters
j++; //increment counter of characters actually printed
}
}
Output:
S H A
H B A
Z A Y
Note the use of the j
variable, and how it is incremented with j++
only after actually printing a character.
answered Nov 9 at 16:55
landru27
752213
752213
if (*(p+i)==0) continue; //don't try to print the trailing ''
I donot understand this point , i mean why donot i say thay if *(p+j)=='0'??
– Âftãb Bãlôçh
Nov 9 at 17:06
'0'
is very different from0
;''
on the other hand equates to0
– landru27
Nov 9 at 17:13
you could use eitherif (*(p+i)==0)
orif (*(p+i)=='')
, but notif *(p+j)=='0'
, which is what you have in your comment
– landru27
Nov 9 at 17:15
ok got it thank you
– Âftãb Bãlôçh
Nov 9 at 17:20
can you help me understand this code please your explanation is tremendous#include<stdio.h> int main() { void ArrPrintMatrix(char (*p)[8]);//function declaration char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation ArrPrintMatrix(matrix);//calling function with base address } void ArrPrintMatrix(char (*p)[8]) { int i = 0, j = 0, k = 0; while (k != 9) { if (p[i][j] == '') { ++i; j = 0; } printf("%c ", p[i][j++]); ++k; if(k%3 == 0) printf("n"); } }
– Âftãb Bãlôçh
Nov 9 at 17:21
|
show 10 more comments
if (*(p+i)==0) continue; //don't try to print the trailing ''
I donot understand this point , i mean why donot i say thay if *(p+j)=='0'??
– Âftãb Bãlôçh
Nov 9 at 17:06
'0'
is very different from0
;''
on the other hand equates to0
– landru27
Nov 9 at 17:13
you could use eitherif (*(p+i)==0)
orif (*(p+i)=='')
, but notif *(p+j)=='0'
, which is what you have in your comment
– landru27
Nov 9 at 17:15
ok got it thank you
– Âftãb Bãlôçh
Nov 9 at 17:20
can you help me understand this code please your explanation is tremendous#include<stdio.h> int main() { void ArrPrintMatrix(char (*p)[8]);//function declaration char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation ArrPrintMatrix(matrix);//calling function with base address } void ArrPrintMatrix(char (*p)[8]) { int i = 0, j = 0, k = 0; while (k != 9) { if (p[i][j] == '') { ++i; j = 0; } printf("%c ", p[i][j++]); ++k; if(k%3 == 0) printf("n"); } }
– Âftãb Bãlôçh
Nov 9 at 17:21
if (*(p+i)==0) continue; //don't try to print the trailing ''
I donot understand this point , i mean why donot i say thay if *(p+j)=='0'??– Âftãb Bãlôçh
Nov 9 at 17:06
if (*(p+i)==0) continue; //don't try to print the trailing ''
I donot understand this point , i mean why donot i say thay if *(p+j)=='0'??– Âftãb Bãlôçh
Nov 9 at 17:06
'0'
is very different from 0
; ''
on the other hand equates to 0
– landru27
Nov 9 at 17:13
'0'
is very different from 0
; ''
on the other hand equates to 0
– landru27
Nov 9 at 17:13
you could use either
if (*(p+i)==0)
or if (*(p+i)=='')
, but not if *(p+j)=='0'
, which is what you have in your comment– landru27
Nov 9 at 17:15
you could use either
if (*(p+i)==0)
or if (*(p+i)=='')
, but not if *(p+j)=='0'
, which is what you have in your comment– landru27
Nov 9 at 17:15
ok got it thank you
– Âftãb Bãlôçh
Nov 9 at 17:20
ok got it thank you
– Âftãb Bãlôçh
Nov 9 at 17:20
can you help me understand this code please your explanation is tremendous
#include<stdio.h> int main() { void ArrPrintMatrix(char (*p)[8]);//function declaration char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation ArrPrintMatrix(matrix);//calling function with base address } void ArrPrintMatrix(char (*p)[8]) { int i = 0, j = 0, k = 0; while (k != 9) { if (p[i][j] == '') { ++i; j = 0; } printf("%c ", p[i][j++]); ++k; if(k%3 == 0) printf("n"); } }
– Âftãb Bãlôçh
Nov 9 at 17:21
can you help me understand this code please your explanation is tremendous
#include<stdio.h> int main() { void ArrPrintMatrix(char (*p)[8]);//function declaration char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation ArrPrintMatrix(matrix);//calling function with base address } void ArrPrintMatrix(char (*p)[8]) { int i = 0, j = 0, k = 0; while (k != 9) { if (p[i][j] == '') { ++i; j = 0; } printf("%c ", p[i][j++]); ++k; if(k%3 == 0) printf("n"); } }
– Âftãb Bãlôçh
Nov 9 at 17:21
|
show 10 more comments
up vote
1
down vote
You should use char (*p)[8]
not char* p
The following code
could wrok:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char (*p)[8]);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(matrix);//calling function with base address
}
void ArrPrintMatrix(char (*p)[8])
{
// i will point to one of the strings in the set of strings
// j will point into the string we are inspecting
// k will count how many characters we have printed
int i = 0, j = 0, k = 0;
// we only need to print the first 9 printable characters we find
while (k != 9)
{
// if we have reached the end of an input string (the null-terminator),
// then move on to the next element in the array, and reset
// the string pointer to the beginning of the new string
if (p[i][j] == '') {
++i;
j = 0;
}
// print the character we are now pointing at,
// and increment the string pointer
printf("%c ", p[i][j++]);
// keep count of how many characters we have printed
++k;
// if k is divisible by 3, start a new row
if(k%3 == 0)
printf("n");
}
}
perfectly done i must appreciate bou unfortunately i don't understand how this code is working ..I also have exams on monday :(
– Âftãb Bãlôçh
Nov 9 at 16:59
can you please tech me your code by commenting or something like dry run..I would be thankfull
– Âftãb Bãlôçh
Nov 9 at 17:39
add a comment |
up vote
1
down vote
You should use char (*p)[8]
not char* p
The following code
could wrok:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char (*p)[8]);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(matrix);//calling function with base address
}
void ArrPrintMatrix(char (*p)[8])
{
// i will point to one of the strings in the set of strings
// j will point into the string we are inspecting
// k will count how many characters we have printed
int i = 0, j = 0, k = 0;
// we only need to print the first 9 printable characters we find
while (k != 9)
{
// if we have reached the end of an input string (the null-terminator),
// then move on to the next element in the array, and reset
// the string pointer to the beginning of the new string
if (p[i][j] == '') {
++i;
j = 0;
}
// print the character we are now pointing at,
// and increment the string pointer
printf("%c ", p[i][j++]);
// keep count of how many characters we have printed
++k;
// if k is divisible by 3, start a new row
if(k%3 == 0)
printf("n");
}
}
perfectly done i must appreciate bou unfortunately i don't understand how this code is working ..I also have exams on monday :(
– Âftãb Bãlôçh
Nov 9 at 16:59
can you please tech me your code by commenting or something like dry run..I would be thankfull
– Âftãb Bãlôçh
Nov 9 at 17:39
add a comment |
up vote
1
down vote
up vote
1
down vote
You should use char (*p)[8]
not char* p
The following code
could wrok:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char (*p)[8]);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(matrix);//calling function with base address
}
void ArrPrintMatrix(char (*p)[8])
{
// i will point to one of the strings in the set of strings
// j will point into the string we are inspecting
// k will count how many characters we have printed
int i = 0, j = 0, k = 0;
// we only need to print the first 9 printable characters we find
while (k != 9)
{
// if we have reached the end of an input string (the null-terminator),
// then move on to the next element in the array, and reset
// the string pointer to the beginning of the new string
if (p[i][j] == '') {
++i;
j = 0;
}
// print the character we are now pointing at,
// and increment the string pointer
printf("%c ", p[i][j++]);
// keep count of how many characters we have printed
++k;
// if k is divisible by 3, start a new row
if(k%3 == 0)
printf("n");
}
}
You should use char (*p)[8]
not char* p
The following code
could wrok:
#include<stdio.h>
int main()
{
void ArrPrintMatrix(char (*p)[8]);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(matrix);//calling function with base address
}
void ArrPrintMatrix(char (*p)[8])
{
// i will point to one of the strings in the set of strings
// j will point into the string we are inspecting
// k will count how many characters we have printed
int i = 0, j = 0, k = 0;
// we only need to print the first 9 printable characters we find
while (k != 9)
{
// if we have reached the end of an input string (the null-terminator),
// then move on to the next element in the array, and reset
// the string pointer to the beginning of the new string
if (p[i][j] == '') {
++i;
j = 0;
}
// print the character we are now pointing at,
// and increment the string pointer
printf("%c ", p[i][j++]);
// keep count of how many characters we have printed
++k;
// if k is divisible by 3, start a new row
if(k%3 == 0)
printf("n");
}
}
edited Nov 9 at 19:40
landru27
752213
752213
answered Nov 9 at 16:38
Yunbin Liu
1,006313
1,006313
perfectly done i must appreciate bou unfortunately i don't understand how this code is working ..I also have exams on monday :(
– Âftãb Bãlôçh
Nov 9 at 16:59
can you please tech me your code by commenting or something like dry run..I would be thankfull
– Âftãb Bãlôçh
Nov 9 at 17:39
add a comment |
perfectly done i must appreciate bou unfortunately i don't understand how this code is working ..I also have exams on monday :(
– Âftãb Bãlôçh
Nov 9 at 16:59
can you please tech me your code by commenting or something like dry run..I would be thankfull
– Âftãb Bãlôçh
Nov 9 at 17:39
perfectly done i must appreciate bou unfortunately i don't understand how this code is working ..I also have exams on monday :(
– Âftãb Bãlôçh
Nov 9 at 16:59
perfectly done i must appreciate bou unfortunately i don't understand how this code is working ..I also have exams on monday :(
– Âftãb Bãlôçh
Nov 9 at 16:59
can you please tech me your code by commenting or something like dry run..I would be thankfull
– Âftãb Bãlôçh
Nov 9 at 17:39
can you please tech me your code by commenting or something like dry run..I would be thankfull
– Âftãb Bãlôçh
Nov 9 at 17:39
add a comment |
up vote
0
down vote
What you are missing is that there is a trailing ''
at the end of SHAHBAZ
, which you are also "printing", but because ''
does not have a character representation, you are seeing what looks like an 'extra' space.
Here is the smallest change I can think of to address this exact problem; add:
if (*(p+i)==0) continue; //don't try to print the trailing ''
just above your existing line:
printf("%c ",*(p+i));//prininting chracters
Output:
S H A
H B A
Z A
There are other things I would do differently than how you are doing them, but this addresses your exact question, using your coding style.
oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
– Âftãb Bãlôçh
Nov 9 at 16:53
add a comment |
up vote
0
down vote
What you are missing is that there is a trailing ''
at the end of SHAHBAZ
, which you are also "printing", but because ''
does not have a character representation, you are seeing what looks like an 'extra' space.
Here is the smallest change I can think of to address this exact problem; add:
if (*(p+i)==0) continue; //don't try to print the trailing ''
just above your existing line:
printf("%c ",*(p+i));//prininting chracters
Output:
S H A
H B A
Z A
There are other things I would do differently than how you are doing them, but this addresses your exact question, using your coding style.
oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
– Âftãb Bãlôçh
Nov 9 at 16:53
add a comment |
up vote
0
down vote
up vote
0
down vote
What you are missing is that there is a trailing ''
at the end of SHAHBAZ
, which you are also "printing", but because ''
does not have a character representation, you are seeing what looks like an 'extra' space.
Here is the smallest change I can think of to address this exact problem; add:
if (*(p+i)==0) continue; //don't try to print the trailing ''
just above your existing line:
printf("%c ",*(p+i));//prininting chracters
Output:
S H A
H B A
Z A
There are other things I would do differently than how you are doing them, but this addresses your exact question, using your coding style.
What you are missing is that there is a trailing ''
at the end of SHAHBAZ
, which you are also "printing", but because ''
does not have a character representation, you are seeing what looks like an 'extra' space.
Here is the smallest change I can think of to address this exact problem; add:
if (*(p+i)==0) continue; //don't try to print the trailing ''
just above your existing line:
printf("%c ",*(p+i));//prininting chracters
Output:
S H A
H B A
Z A
There are other things I would do differently than how you are doing them, but this addresses your exact question, using your coding style.
answered Nov 9 at 16:45
landru27
752213
752213
oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
– Âftãb Bãlôçh
Nov 9 at 16:53
add a comment |
oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
– Âftãb Bãlôçh
Nov 9 at 16:53
oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
– Âftãb Bãlôçh
Nov 9 at 16:53
oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
– Âftãb Bãlôçh
Nov 9 at 16:53
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229661%2fc-programming-problem-pointers-and-arrays-2d%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Nasty question. It tells you to write a function like this
ArrPrintMatrix(char *(p)[7])
and gives you a string that's 8 characters long ("SHAHBAZ" plus a terminating zero).– Tim Randall
Nov 9 at 16:42
thank you so much for quick reply . I donot understand hot to use
ArrPrintMatrix(char *(p)[7])
and to pass it to function can you please please help me ....please– Âftãb Bãlôçh
Nov 9 at 16:46