Thread stopped...access violation at 0x6: read of address 0x6 [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am getting an error saying: Thread stopped.....access violation at 0x6:read of address 0x6.
However my code doesn't have such address.
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j,A[4][5],c=0;
for(j=0;j<5;j++)
for(i=0;j<4;i++)
{
A[i][j]=c;
c++;
}
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
printf("%d\t",A[i][j]);
printf("\n");
}
getch();
}
I was asked to enter a double array column by column instead of row by row, and I want to verify my method so I tried to print it out but had no luck doing so.

for(i=0;j<4;i++)
^^
Shouldn't this be:
for(i=0;i<4;i++)
j<4 is always true once it enters inner the loop for the first time and is the reason for memory access violation.

Related

How does the code that prints array elements works? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.

How to copy all chars(include '\0') from one array to another without using strcpy function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
copy all characters from one character array to another without using strcpy function.
char s1[80],s2[80];
int i;
printf("input s2");
scanf("%s",&s2);
for(i=0;i<=strlen(s2);i++)
s1[i]=s2[i];
printf("s1:%s",s1);
Instead of scanf, using gets function for getting input with spaces.
#include<stdio.h>
int main()
{
char s1[80],s2[80];
int i;
printf("input s2");
// scanf("%c",&s2);
gets(s2);
printf("%d",strlen(s2));
for(i=0;i<strlen(s2);i++)
{
s1[i]=s2[i];
}
printf("s1:%s",s1);
}

A function in printf in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can I call a function that has printf in it - in printf BUT without it printing the value entered twice?
in the code written below is how it is now, it prints the inserted value.
can I just use the function without printing the value again?
can I use printf(roll(num))
#include<stdio.h>
int roll(int);
int main()
{
int num;
printf("%d",roll(num));
return 0;
}
int roll(int a)
{
printf("Enter a number between 1-6:\n");
scanf("%d",&a);
while(a<1 || a>6)
{
printf("Wrong input! please enter a number between 1-6:\n");
scanf("%d",&a);
}
return a;
}
Just call the function without printf.
roll(num);

Why does this print 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
int var=0;
for(; var++; printf("%d",var));
printf("%d", var);
}
Please explain to me this C code. How is the output 1?
You might be confused because of the wrong code indentation. Your code is:
for(; var++; printf("%d",var))
;
printf("%d", var);
So you always get the output of the second printf. As var is initialized to 0 and var++ (the for-condition) is always executed, you end up with var==1.

Unable to obtain Matrix in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Following is the code to obtain matrix like 11 12 13 etc... I am not getting desired results since long. Help me with the solution.
#include<stdio.h>
int main() {
int i,j;
for(i=1;i<5;i++){
for(j=1;j<5;j++){
printf("%d %d",i,j);
}
printf("\n");
}
fgetc(stdin);
}
Probably because you have a space between the digits:
printf("%d %d",i,j);
Shouldn't it be:
printf("%d%d ",i,j);
for your desired output?
Shouldn't printf("%d %d",i,j); actually be printf("%d%d ",i,j);?

Resources