variable-assignment

PHP Logical Operators precedence affects variable assignment results strangely

北城余情 提交于 2019-12-02 16:16:25
问题 $var4 = 123; function fn1($p1) { return array('p1' => 1, 'p2' => 2); } if ($var1 = fn1(1) AND $var4 == 123) { print_r($var1); } if ($var2 = fn1(1) && $var4 == 123) { print_r($var2); } if (($var3 = fn1(1)) && $var4 == 123) { print_r($var3); } If you run this simple script it will output strange results, at least for me!! First output from first if expression will result in an array returned from the function & assigned to the $var1 variable, which is what I'm expecting, well? Second output

Why is x == (x = y) not the same as (x = y) == x?

淺唱寂寞╮ 提交于 2019-12-02 15:44:51
Consider the following example: class Quirky { public static void main(String[] args) { int x = 1; int y = 3; System.out.println(x == (x = y)); // false x = 1; // reset System.out.println((x = y) == x); // true } } I'm not sure if there is an item in the Java Language Specification that dictates loading the previous value of a variable for comparison with the right side ( x = y ) which, by the order implied by brackets, should be calculated first. Why does the first expression evaluate to false , but the second evaluate to true ? I would have expected (x = y) to be evaluated first, and then it

Why do object assignments refer to memory locations when primitive types don't? [duplicate]

痞子三分冷 提交于 2019-12-02 15:39:33
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed 3 years ago . I have a bit of an odd question regarding how primitive types work in Java. When using Objects when you assign an ObjectA to be ObjectB be like such Rectangle ObjectB = new Rectangle(); ObjectA = ObjectB; Any calls to ObjectA refer now to ObjectB's memory location. However when using integers or other primitive types this is not the case. For example int x = 3; int y = x;

Assign a print statement to a variable in a function in Python 2.7

馋奶兔 提交于 2019-12-02 13:36:11
I'm trying to assign a print statement to a variable in a function: def namer(fn, ln='Smith'): # return value, default value r = print "Your name is ", fn, ln return r But when I run the module, it says: Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> import m8 File "m8.py", line 3 r = print "Your name is ", fn, ln ^ SyntaxError: invalid syntax Any explanation? As you noted, in Python2.x, print is a statement. A statement is not an object, you cannot assign it to anything, you can just execute it. Why would you want to return the print statement? Why not the string

Javascript - [Why?] Assigning a variable to an object [duplicate]

谁说我不能喝 提交于 2019-12-02 12:46:27
This question already has an answer here: Is JavaScript a pass-by-reference or pass-by-value language? 29 answers Why is it that when I assign a variable to an object and make a change to that variable it also changed the objects? For example: c = 26; a = b = c; a += 1; a // 27 b // 26 c // 26 but z = {}; x = y = z; x.ab = 5; x // Object {ab: 5} y // Object {ab: 5} z // Object {ab: 5} Why (in the example above) does y.ab and z.ab exist? I only modified x not y or z . Howcome in the first example (with the integers), when I changed the value of a , b and c weren't affected? When you assign an

C assign string from argv[] to char array

↘锁芯ラ 提交于 2019-12-02 12:18:35
I have the following code which reads an file name from the command line and opens this file: #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv){ FILE *datei; char filename[255]; //filename = argv[1]; //datei=fopen(filename, "r"); datei=fopen(argv[1], "r"); if(datei != NULL) printf("File opened"); else{ printf("Fehler beim öffnen von %s\n", filename); return EXIT_FAILURE; } return EXIT_SUCCESS; } This example works, but I want to write the string from the command line to the char array and pass that char array to to fopen(), but i get the compiler error Error: assignment to

Assignment makes pointer without a cast

£可爱£侵袭症+ 提交于 2019-12-02 11:27:38
I am editing a quick sort code so that the values of low, high, and middle point to an array element instead of integers. This is my code: #include <stdio.h> #define N 10 void quicksort(int a[], int *low, int *high); int split(int a[], int *low, int *high); int main(void) { int a[N], i; printf("Enter %d numbers to be sorted: ", N); for (i=0; i<N; i++) scanf("%d", &a[i]); quicksort(a, &a[0], &a[N-1]); printf("In sorted order: "); for (i=0; i<N; i++) printf("%d ", a[i]); printf("\n"); return 0; } void quicksort(int a[], int *low, int *high) { int *middle; if (low >= high) return; middle = split

How declaration of variables behave?

 ̄綄美尐妖づ 提交于 2019-12-02 11:11:50
#include<stdio.h> #include<conio.h> int main(){ char i; int c; scanf("%i",&c); scanf("%c",&i);// catch the new line or character introduced before x number printf("%i",i);// value of that character getch(); return(0); } The program will behave in the same way with the next variable declarations instead of the above variable declaration: this: int c; int *x; int i; or this: int *x; int c; int i; And only this way: c variable and a x pointer before the i variable. I know that those last declarations haven't sense, the int i instead of char i , and an added pointer that isn't even needed. But

Cannot assign value to global array in c++

让人想犯罪 __ 提交于 2019-12-02 10:29:30
问题 I've got this code: #include <iostream> int tabela[1]; tabela[0] = 1; int main(){ std::cout << tabela[0]; std::cin.get(); return 0; } and it doesn't want to work. My compiler says " "tabela" doesn't name a type". However if I do this: #include <iostream> int tabela[1]; int main(){ tabela[0] = 1; std::cout << tabela[0]; std::cin.get(); return 0; } It works. Can sb explain me why? Thanks in advance. 回答1: At the outermost level, a C++ file is a sequence of declarations. tabela[0] = 1; is not a

matlab: filling matrix diagonalwise [duplicate]

ぃ、小莉子 提交于 2019-12-02 10:17:53
This question already has an answer here: adding values to diagonals of matrix using element-wise addition in matlab 3 answers I have an (2n-1)-by-1 vector with certain values and I want to obtain an n-n matrix with the diagonals filled using the same value. Eg. if I have a = [1; 2; 3; 4; 5]; I want to obtain A = [[3 4 5];[2 3 4];[1 2 3]] = 3 4 5 2 3 4 1 2 3 My matrix dimensions are a lot bigger so I'd want this as efficient as possible. I already found following solutions: n = 3; A = toeplitz(a); A = A(1:n,end-n+1:end) and A = a(n)*eye(n); for j=1:n-1 A(1+j:n+1:end-j*n) = a(n-j); A(j*n+1:n+1