C programming problem pointers and arrays 2d











up vote
1
down vote

favorite
1












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

}
}









share|improve this question
























  • 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















up vote
1
down vote

favorite
1












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

}
}









share|improve this question
























  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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

}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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
















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












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.






share|improve this answer





















  • 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










  • 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










  • 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




















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");
}
}





share|improve this answer























  • 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


















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.






share|improve this answer





















  • oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
    – Âftãb Bãlôçh
    Nov 9 at 16:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • 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










  • 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










  • 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

















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.






share|improve this answer





















  • 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










  • 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










  • 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















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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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










  • 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












  • '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










  • 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














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");
}
}





share|improve this answer























  • 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















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");
}
}





share|improve this answer























  • 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













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");
}
}





share|improve this answer














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");
}
}






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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










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.






share|improve this answer





















  • oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
    – Âftãb Bãlôçh
    Nov 9 at 16:53















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.






share|improve this answer





















  • oh man !!! Thank you so much ..... THANKS DUDE LOVE YOU
    – Âftãb Bãlôçh
    Nov 9 at 16:53













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check