Pointer taking input infinitely - c

I have following C program:
#include <stdio.h>
int main()
{
char p[12], i;
for(i=0; i<12; i++)
{
scanf("%d", p+i);
}
for(i=0; i<12; i++)
{
printf("%d", *(p+i));
}
return 0;
}
This code is taking input infinitely. Whats wrong with this?

You are messing up int and char (format specifier %d is for integers, not characters). Please refer to here for more info of format specifiers for different types.
If you want to read integers from user, you need to change your code to:
#include <stdio.h>
int main()
{
int p[12], i;
for(i=0; i<12; i++)
{
scanf("%d", &p[i]);
}
for(i=0; i<12; i++)
{
printf("%d", *(p+i)); // or better to use p[i] here
}
return 0;
}

The counter i is declared as a character in your code. This will lead to a logical error while executing the for loop.
So decare the variable i as follows,
int i;
Also change the format specifier to %c to handle the input properly.

Related

2nd element in array (int array) isn't being processed in outside function for some reason

I'm new to programming and started learning structs. I am making a program that asks for a student name, 3 test results, and then those results get passed through a function that gets rid of the lowest mark.
When I pass it through this function for some reason it just doesn't show the 2nd element and replaces it with the 3rd element while the 3rd element is replaced with a 0.
I've altered my code to show what I mean. If you could be so kind as to copy and paste to see what I mean. Any help would be greatly appreciated.
#include <stdlib.h>
#define SIZE 1
#define TESTS 3
typedef struct
{
char name[50];
int test[TESTS];
float avg;
}Results;
void best(Results *marks[TESTS]);
int main()
{
Results nameArr[SIZE];
for(int i=0; i<SIZE; i++)
{
printf("Enter name for student %d: ", i+1);
scanf("%\s", nameArr[i].name);
printf("Enter marks (Out of 100) for %s:\n", nameArr[i].name);
for(int j=0; j<TESTS; j++)
{
printf("Test %d: ", j+1);
scanf("%d", &nameArr[i].test[j]);
}
}printf("\n");
for(int i= 0; i<TESTS; i++)
{
printf("Test %d in main() = %d\n", i+1, nameArr[0].test[i]);
}printf("\n");
for(int i= 0; i<SIZE; i++)
{
best(&nameArr[i].test);
}
printf("\n");
system("PAUSE");
return 0;
}
void best(Results *marks[TESTS])
{
for(int i= 0; i<TESTS; i++)
{
printf("Test %d in function = %d\n", i+1, marks[i]);
}
}
The type of nameArr[i].test is int[TESTS], not Results*[TESTS]. Use correct type for the function argument.
Also arrays in expression are automatically converted to pointers (some exceptions exist), so you don't need & before arrays (at least in this case).
Some other points:
You should add #include <stdio.h> to use printf() and scanf().
\s is an invalid escape sequence. You should use "%s" instead of "%\s".
#include <stdio.h> /* add this */
#include <stdlib.h>
#define SIZE 1
#define TESTS 3
typedef struct
{
char name[50];
int test[TESTS];
float avg;
}Results;
/* fix argument type */
void best(int marks[TESTS]);
/* void best(Results *marks[TESTS]); */
int main()
{
Results nameArr[SIZE];
for(int i=0; i<SIZE; i++)
{
printf("Enter name for student %d: ", i+1);
/* remove invalid escape sequence */
scanf("%s", nameArr[i].name);
/* scanf("%\s", nameArr[i].name); */
printf("Enter marks (Out of 100) for %s:\n", nameArr[i].name);
for(int j=0; j<TESTS; j++)
{
printf("Test %d: ", j+1);
scanf("%d", &nameArr[i].test[j]);
}
}printf("\n");
for(int i= 0; i<TESTS; i++)
{
printf("Test %d in main() = %d\n", i+1, nameArr[0].test[i]);
}printf("\n");
for(int i= 0; i<SIZE; i++)
{
/* remove & */
best(nameArr[i].test);
/* best(&nameArr[i].test); */
}
printf("\n");
system("PAUSE");
return 0;
}
/* fix argument type */
void best(int marks[TESTS])
/* void best(Results *marks[TESTS]) */
{
for(int i= 0; i<TESTS; i++)
{
printf("Test %d in function = %d\n", i+1, marks[i]);
}
}
As MikeCAT pointed out in the comments, you are invoking undefined behavior with your printf() statements. Argument of type "%d" expects a decimal value but you are giving it a pointer to a struct which would expect an argument of "%p". I would recommend you research the printf() function here: https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
This will help you get a better understanding of how to use it to accurately and appropriately print the values you would like to see.

Why does scanf function asks for an extra input?

**#include <stdio.h>
#define SIZE 3
void scanA(int array[SIZE][SIZE]); // prototyping
int main()
{
int myarray[SIZE][SIZE];
int i,j;
printf("Please enter the array: \n");
scanArray(myarray);
for(i=0; i<SIZE; i++)
for(j=0; j<SIZE; j++)
{
printf("%c",myarray[i][j]);
}
printf("\n");
return 0;
}
void scanA(int array[SIZE][SIZE]) // function defintion
{
int i;
int j;
for(i=0; i<SIZE; i++) // looping to scan
for(j=0; j<SIZE; j++)
{
scanf("%c\n ",&array[i][j]);
}
}**
//The scanf in the scanA function asks for 10 chars although it is looped 9 times
I want to know the reason and a solution.
Try to put a blank space before the %c (only when you read a char), cause sometimes scanf reads the enter as a char. (I'm not totally sure about it, but with me this solution works)
Sorry for my english, it isn't my main language :(

Find common value from two arrays

I am attempting to write a program which examines values in two arrays of different sizes and adds the common elements into a third array.
I am using the following code:
#include <stdio.h>
int main(void) {
// your code goes here
int A[5], B[8], i, j, s=1;
int* c;
c=(int*)malloc(s*sizeof(int));
for(i=0;i<5;i++)
{scanf("\t %d", &A[i]);}
printf("\n");
for(j=0;j=8;j++)
{scanf("\t %d", &B[j]);}
for(i=0, j=0;i<5,j<8;i++,j++)
{
if(A[i]==B[j])
{
c[i]=A[i];
s++;
printf("\n %d", c[i]);
c=realloc(c, s*sizeof(int));
break;
}
}
return 0;
}
but when I try to execute it, it is giving the problem that the time limit has been exceed. What is causing this problem? For compilation I am using the on-line compiler ideone.
Below is the code that would work.
Except for the mentioned problems in the for loops, assignment to array c was wrong.
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int A[5], B[8], i, j, s=1;
int* c;
c=(int*)malloc(s*sizeof(int));
for(i=0;i<5;i++) {scanf("\t %d", &A[i]);}
printf("\n");
for(j=0;j<8;j++) {scanf("\t %d", &B[j]);}
for(i=0;i<5;i++){
for(j=0;j<8;j++){
if(A[i]==B[j]){
c[s-1] = A[i]; // use s-1 as an index
printf("\n %d", c[s-1]);
s++;
c=(int*)realloc(c, s*sizeof(int));
}
}
}
return 0;
}
Hmmm...where to begin?
Are you only looking for characters that are the same AND in the same position? It LOOKS as though that is what you are trying to do, but as the array A and B have different dimensions, you are going to get into some trouble. And are you ONLY looking for the FIRST match? With the "break", you will stop after the first match.
Assuming you are looking for ALL element common to both lists -- in ANY position -- you will want code more like the following:
#include <stdio.h>
int main(void)
{
int A[5], B[8], C[8];
for (int i = 0; i < 5; i++)
scanf(" %d ", &A[i]);
printf("\n");
for (int i = 0; i < 8; i++)
scanf(" %d ", &B[i]);
int ci = 0;
for (int ai = 0; ai < 5; ai++)
{
for (int bi = 0; bi < 8; bi++)
{
if (A[ai] == B[bi])
{
C[ci] = A[ai];
ci++;
break;
}
}
}
C[ci] = 0;
printf("Common chars: %s\n", C);
return 0;
}
The key to what you want, I think, is the nested for loops, which iterate through EACH of the strings (A and B) separately, while looking for matching characters.

exercise about vector and function

Thanks a lot, with you help I understood all my mistakes (:
This is my first time using this website so I'm not sure if it is in the right format.
Basically I have to make a function that fills a vector, but it isn't working at all.
English isn't my first language so this is probably really confusing, but I'd appreciate if
somebody helped me. Thanks a lot.
#include <stdio.h>
#include <stdlib.h>
void le_vet(int v[1000], int n, int i)
{
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
int main()
{
int v[1000], n;
printf("Type the syze of the vector: ");
scanf("%d", &n);
void le_vet(n);
system ("pause");
return 0;
}
You are not calling le_vet in your main function, you are rather doing something more along the lines of creating a function pointer called "le_vet" that takes an int (by default, as no type is specified) and returns a void. I'm pretty sure this is not what's intended.
Instead, change void le_vet(n) to le_vet(v, n) and change this:
void le_vet(int v[1000], int n, int i)
{
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
to this:
void le_vet(int v[], int n)
{
int i;
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
Since you're not needing to pass i in from outside the function, there's no need to include it in the arguments to the function. The first element in a for loop is executed once right as the loop is entered, therefore it is often used to declare the iteration variable for the loop, as I did here.
EDIT: Whoops. Can't do that in C. I'm to used to C++ that I made a goof here. Declare i just above the loop, as #Over Flowz suggests. Updating my revised code, but leaving this record as evidence that it's time to stop working and go eat dinner :)
You are only passing one argument to le_vet(), when it requires three arguments. You also need to remove the void, since you are calling on a function.
Maybe this will work.
void le_vet(int n)
{
static int v[1000];
for (int i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
You don't need the int i passed as a parameter, since you are creating another one in the for loop.
int i = 0;
while (i < n)
{
i++;
}
is the same as
for (int i = 0; i < n; i++)
When you invoke like this:
...
scanf("%d", &n);
void le_vet(n); //you are declaring a function. You need to remove the void keyword
system ("pause");
...
You should invoke like this:
...
scanf("%d", &n);
le_vet(n);
system ("pause");
...
Then you will see the real errors, like the number of parameters
try:
#include <stdio.h>
#include <stdlib.h>
void le_vet(char v[], int n)
{
int i = 0;
for(i = 0; i < n; i++)
{
printf("Type the number %d: ", i+1);
scanf("%s", &v[i]); //Read string, not decimal for output.
}
}
int main()
{
char v[1000] = {0}, n;
printf("Type size of the vector: ");
scanf("%d", &n);
le_vet(v, n);
printf("%s", v);
system("pause");
return 0;
}
Hope it helps.

Using scanf() to input a square of integers

I am coding a c project, that needs that the user enters a N*N square of integers : that's to say an input of N lines of N integers. The algo works fine.
Now I want the user to input N lines of N integers each of consecutive integers are separated by a space. Here, I don't have the right usage of scanf, because I tried to declare integers array but I was not able to deal with the spacing.
I tried something like this, very unnatural and failing :
int i=0;
int j=0;
int N;
scanf("%d",&N);
char c[N][2*N-1];
while(i < N){
scanf("%s",&c[i]);
i++;
}
i=0;
j=0;
while (i<N){
while (j<N){
c[i][j]=c[i][2*j]-48;
j++;
}
j=0;
i++;
}
Can someone help ?
Best,
Newben
If I understood what your original code was supposed to do then this code should actually do it (and print it out again just to prove it worked).
You need to dynamically allocate the array since it's variable size ( In C99 you could use variable sized arrays on the stack but that's really a different discussion ).
scanf will automatically ignore white-space between the integers (including spaces and new-lines) so you don't need to parse that out manually.
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i=0, j=0, N;
int **c;
scanf("%d",&N);
c = malloc(N*sizeof(int*));
for (i=0;i<N;i++)
{
c[i] = malloc(N*sizeof(int));
for (j=0;j<N;j++)
{
scanf("%d",&c[i][j]);
}
}
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
free(c[i]);
}
free(c);
return 0;
}
C99 alternative without malloc/free for completeness (I'v never liked this C99 feature since there's no way to check that the was/is enough space on the stack):
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i=0, j=0, N;
scanf("%d",&N);
int c[N][N];
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
scanf("%d",&c[i][j]);
}
}
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}

Resources