This question already has answers here :
To have correct output you should change
int temp[size]
to int* temp = *tableau
or to int* temp = (int*) malloc(sizeof(**tableau) * size)
.
These solutions work because *tableau
and or the memory allocated by malloc
is not destroyed after invertTable
.
Normally temp
should be destroyed after invertTable
function and making tableau[0]
a dangling pointer then the system may reallocate the memory where temp
pointed to. Thus, this part of memory may now contain random data. These data are probably what you got at execution time.
printf is changing string contents, local variables are stored in an area of memory called the stack. Then you pass and modify these char arrays to/in your functions. And since main is the last function that gets terminated, arr in this case doesn't go out of scope until main� A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Your issue is not related to printf, it's due to a bug in your code where you try to use memory you're not supposed to.
In this line in your invertTable
function:
tableau[0]=temp;
You are pointing the table
pointer in your main() function to the local temp
variable.
Your temp
array goes out of scope when the invertTable
function ends, so you end up with a dangling pointer and you can't use that memory anymore- doing so is undefined behavior.
You could instead dynamically allocate memory, which will stay valid after invertTable
ends:
int *temp = malloc(sizeof(int) * size);
for(int i = 0; i < size; i++)
{
temp[i]=-1*tableau[0][i];
}
//deallocate previous allocation
free(tableau[0]);
tableau[0]=temp;
Local, Global and Static variables in C, Local Variables # The variables which are declared inside the function, Last updated on July 27, 2020 b; // you can use a and b within braces only } void function_2() { printf("%d\n", Any function can access and modify global variables. Secrets of “printf” Professor Don Colton Brigham Young University Hawaii printf is the C language function to do format-ted printing. The same function is also available in PERL. This paper explains how printfworks, and how to design the proper formatting specification for any occasion. 1 Background In the early days, computer programmers
Your problem is not related to printf
it's indeed invertTable
which is the culprit.
When dealing with arrays table
is equal to &table[0]
so in this case, you don't need to send the address of tableau.
#include <stdio.h>
#include <stdlib.h>
void invertTable(int *tableau,int size){
for(int i = 0; i < size; i++)
{
tableau[i] = -1 * tableau[i];
}
}
void test(){
}
int main(int argc, char const *argv[])
{
int* table = (int*) malloc(5 * sizeof(int));
table[0]=1;
table[1]=2;
table[2]=3;
table[3]=4;
table[4]=5;
invertTable(table,5);
test();
for(int i = 0; i < 5; i++)
{
//Here is the problem
printf("\n %d \n",table[i]);
}
free(table);
return 0;
}
this will do what you are looking for. Plus, you don't need to use any temporary variable either.
By the way, temp
was temporary, it was not allocated on the heap so it was destroyed when invertTable
returned.
Printf(), As the printf buffer is of a fixed size, and wraps, you will lose the earlier output. As the lines can also In the case of multiple GPUs, you will see a noticeable change in the clock values. Thus, it's quite easy printf_ruby.rb: #!/usr/local/bin/ ruby. The main point here is that you cannot return a local array from the function. The memory that was used by array toreturn is free after the function call. It can be overwritten by any function call, including printf. Use malloc to allocate memory for your array in the function and return a pointer to the allocated memory, or pass array as a
tableau[0]=temp;
This is not valid. It means to return a pointer to a local array. This is undefined behavior.
You can do so:
for(int i = 0; i < size; i++)
(*tableau)[i]*=-1;
C variable with examples, The value of the C variable may get change in the program. C variable might be The scope of local variables will be within the function only. These variables are printf("All variables are accessed from main function");. printf("\nvalues:� The variables declared using const keyword, get stored in .rodata segment, but we can still access the variable through the pointer and change the value of that variable. By assigning the address of the variable to a non-constant pointer, We are casting a constant variable to a non-constant pointer.
Defining Functions, A valid function name is like a valid variable name: a sequence of letters, The printf statement (see Input and Output) simply tells Octave to print the string "\a" . To print a message along with ringing the bell, you might modify the wakeup to look like this: Variables used in the body of a function are local to the function. Sr.No. Flags & Description; 1-Left-justify within the given field width; Right justification is the default (see width sub-specifier). 2 + Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By defa
C Programming - Functions, The C language is similar to most modern programming languages in that it allows the in (referring to that information with local symbolic names called parameters), does some printf("Congratulations on your %d th Birthday\n", age ); changes made to the parameter also modify the original variable containing the data. printf uses the format argument as an indicator. Actually, most of the magic is done in vprintf. printf is only a wrapper for vprintf which write the output string to stdout. I suggest you read vprint's GNU implementation, it only has 2278 line of code ;) I said earlier that the format string is used as an indicator to the amount of variables.
what is the difference between static int and int?, Hence, static variables preserve their previous value in their previous scope and if we declare an int variable in a function, then that variable is a local variable for #include <stdio.h> void func(){ int i = 0; i++; printf("i = %d\n",i); } int main(){� A global variable’s value can be changed through a function without passing it as an argument. That’s why they are commonly used in the first place :- See the following :- [code]#include <stdio.h> int c = 0; //Global Variable void func(){ pri
Comments When you assign tableau[0] = temp; the variable temp is local so it is freed after function returns. So your table points to an unallocated memory. Then the behavior is undefined, the call to printf probably reallocates some memory where temp was, so you see the junk... See geeksforgeeks.org/return-local-array-c-function similar to why array values are different from assigned values c program