variable-assignment

Why can you assign an integer value to an uninitialized pointer

为君一笑 提交于 2019-12-10 15:54:53
问题 When I do this, it prints out "2" perfectly. int main() { int *p; int x = 2; *p = x; cout << *p; } But when I first initialized *p to be null, the program crashes. int main() { int *p=0; int x = 2; *p = x; cout << *p; } I want to ask what does the first program even successfully run in the first place, why can a value be assigned to an uninitialized pointer? [EDIT] My question is actually related to this past exam question that I got. You can tick more than one answer and it seems (b) & (c)

Assigning a value in exception handling in R

て烟熏妆下的殇ゞ 提交于 2019-12-10 15:48:56
问题 while(bo!=10){ x = tryCatch(getURLContent(Site, verbose = F, curl = handle), error = function(e) { cat("ERROR1: ", e$message, "\n") Sys.sleep(1) print("reconntecting...") bo <- bo+1 print(bo) }) print(bo) if(bo==0) bo=10 } I wanted to try reconnecting each second after the connection failed. But the new assignment of the bo value is not effective. How can i do that? Or if you know how to reconnect using RCurl options (I really didn't find a thing) it would be amazing. Every help is

Java constructor final variable assignment

北城余情 提交于 2019-12-10 15:28:04
问题 public class User { private final String _first_name; private final String _last_name; private final String _org_ID; private final TimeZone _time_zone; private final InternetAddress _email; private final Date _last_login; private final Date _creation_date; public User( final String org_ID, final String username, final String first_name, final String last_name, final List<String> roles, final TimeZone time_zone, final InternetAddress email, final Date last_login, final Date creation_date ) {

Algorithm to print to screen path(s) through a text maze

好久不见. 提交于 2019-12-10 15:16:29
问题 For my C++ assignment, I'm basically trying to search through a chunk of text in a text file (that's streamed to my vector vec ) beginning at the second top character on the left. It's for a text maze, where my program in the end is supposed to print out the characters for a path through it. An example of a maze would be like: ############### Sbcde####efebyj ####hijk#m##### #######lmi##### ############### ############### ############### ############### ############### ############### ########

Strange variable assignment

风格不统一 提交于 2019-12-10 14:25:40
问题 I was studying some code I found on the web recently, and came across this php syntax: <?php $framecharset && $frame['charset'] = $framecharset; ?> Can someone explain what is going on in this line of code? What variable(s) are being assigned what value(s), and what is the purpose of the && operator, in that location of the statement? Thanks! Pat 回答1: Ah, I just wrote a blog post about this idiom in javascript: http://www.mcphersonindustries.com/ Basically it's testing to see that

How does the name of an immutable object rebind to the result of an augmented assignment?

大兔子大兔子 提交于 2019-12-10 13:57:26
问题 How does the name of an immutable object rebind to the result of an augmented assignment? For mutable objects, example, if x = [1, 2, 3] , and y = [4, 5], then when we do x += y, it is executed as x.__iadd__(y) which modifies x in place and does the name x rebind to it again? And how does it work when x is immutable? Here's what Python documentation has to say about augmented assignments. If x is an instance of a class that does not define a __iadd__() method, x.__add__(y) and y.__radd__(x)

Can the props in a destructuring assignment be transformed in place?

心不动则不痛 提交于 2019-12-10 13:32:37
问题 This works… const { prop1:val1, prop2:val2 ) = req.query val1 = val1.toLowerCase() Though, I'm more inclined to do something like const { prop1.toLowerCase():val1, prop2:val2 } = req.query or const { prop1:val1.toLowerCase(), prop2:val2 } = req.query neither of which work. Is there a syntax similar to this or must manipulations be done outside of the destructing assignment? 回答1: No, this is not possible. A destructuring assignment does only assign, it does not do arbitrary transformations on

foreach, performance-wise. Should we declare variable once before loop or inside it?

情到浓时终转凉″ 提交于 2019-12-10 13:28:34
问题 What is better for performance wise declaring the variable outside the foreach statment and the each time reassign it in side it (foreach) or create an new variable inside foreach for example private List<ListItem> GetItems() { var items = new List<ListItem>(); var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ListItem item; foreach (var i in collection) { item = new ListItem { Text = i.ToString() }; items.Add(item); } return items; } or this one? private List<ListItem>

How to modify place with arbitrary function

懵懂的女人 提交于 2019-12-10 13:19:51
问题 Sometimes we need to modify a place but here is no built-in function that meets our needs. For instance, here are incf and decf for addition and subtraction: CL-USER> (defvar *x* 5) *X* CL-USER> (incf *x* 3) 8 CL-USER> *x* 8 CL-USER> (decf *x* 10) -2 CL-USER> *x* -2 But how about multiplication and division? What if we wish to modify a place with arbitrary function, like this: (xf (lambda (x) ...) *x*) xf utility would be very useful, especially when we have to deal with deeply nested

how does the assignment symbol work - Ruby

拜拜、爱过 提交于 2019-12-10 12:59:18
问题 In Ruby if i just assign a local variable. sound = "bang". is that a main.sound=("bang") method? if so, where and how is that method "sound=" being defined? or how is that assignment working? if not, what is actually happening? i know that for a setter method you would say x.sound=("bang"). and you are calling the method "sound=" on the object "x" with the argument "bang". and you are creating an instance variable "sound". and i can picture all of that. but not when you assign a variable in